是否可以进行异步 jdbc 调用? [英] Is asynchronous jdbc call possible?

查看:54
本文介绍了是否可以进行异步 jdbc 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道有没有办法异步调用数据库?

I wonder if there is a way to make asynchronous calls to a database?

例如,假设我有一个需要很长时间来处理的大请求,我想发送请求并在请求返回值时接收通知(通过传递侦听器/回调或其他东西).我不想阻塞等待数据库回答.

For instance, imagine that I've a big request that take a very long time to process, I want to send the request and receive a notification when the request will return a value (by passing a Listener/callback or something). I don't want to block waiting for the database to answer.

我不认为使用线程池是一种解决方案,因为它不能扩展,在大量并发请求的情况下,这将产生大量线程.

I don't consider that using a pool of threads is a solution because it doesn't scale, in the case of heavy concurrent requests this will spawn a very large number of threads.

我们正面临网络服务器的这种问题,我们通过使用 select/poll/epoll 系统调用找到了解决方案,以避免每个连接有一个线程.我只是想知道如何在数据库请求中使用类似的功能?

We are facing this kind of problem with network servers and we have found solutions by using select/poll/epoll system call to avoid having one thread per connection. I'm just wondering how to have a similar feature with database request?

注意:我知道使用 FixedThreadPool 可能是一个很好的解决方法,但我很惊讶没有人开发出真正异步的系统(不使用额外线程).

Note: I'm aware that using a FixedThreadPool may be a good work-around, but I'm surprised that nobody has developed a system really asynchronous (without the usage of extra thread).

** 更新 **
由于缺乏真正实用的解决方案,我决定自己创建一个库(finagle的一部分):finagle-mysql.它基本上解码/解码 mysql 请求/响应,并在后台使用 Finagle/Netty.即使有大量连接,它也能很好地扩展.

** Update **
Because of the lack of real practical solutions, I decided to create a library (part of finagle) myself: finagle-mysql. It basically decodes/decodes mysql request/response, and use Finagle/Netty under the hood. It scales extremely well even with huge number of connections.

推荐答案

我不明白将 JDBC 调用包装在 Actor、执行器或其他任何东西中的任何提议方法如何在这里提供帮助 - 有人可以澄清一下.

I don't understand how any of the proposed approaches that wrap JDBC calls in Actors, executors or anything else can help here - can someone clarify.

当然,基本问题是 JDBC 操作阻塞了套接字 IO.当它这样做时,它会阻止它运行的线程 - 故事的结尾.无论您选择使用哪种包装框架,最终都会使每个并发请求保持忙碌/阻塞一个线程.

Surely the basic problem is that the JDBC operations block on socket IO. When it does this it blocks the Thread its running on - end of story. Whatever wrapping framework you choose to use its going to end up with one thread being kept busy/blocked per concurrent request.

如果底层数据库驱动程序(MySql?)提供了一种拦截套接字创建的方法(参见SocketFactory),那么我想有可能在JDBC api之上构建一个异步事件驱动的数据库层,但我们有将整个 JDBC 封装在事件驱动的 Facade 后面,并且该 Facade 看起来不像 JDBC(在它是事件驱动的之后).数据库处理将在与调用者不同的线程上异步发生,您必须弄清楚如何构建不依赖于线程关联的事务管理器.

If the underlying database drivers (MySql?) offers a means to intercept the socket creation (see SocketFactory) then I imagine it would be possible to build an async event driven database layer on top of the JDBC api but we'd have to encapsulate the whole JDBC behind an event driven facade, and that facade wouldn't look like JDBC (after it would be event driven). The database processing would happen async on a different thread to the caller, and you'd have to work out how to build a transaction manager that doesn't rely on thread affinity.

类似于我提到的方法甚至允许单个后台线程处理并发 JDBC exec 的负载.在实践中,您可能会运行一个线程池来使用多个内核.

Something like the approach I mention would allow even a single background thread to process a load of concurrent JDBC exec's. In practice you'd probably run a pool of threads to make use of multiple cores.

(当然,我不是在评论原始问题的逻辑,只是在没有选择器模式的用户的情况下暗示在阻塞套接字 IO 的情况下并发是可能的-更简单的只是计算出典型的 JDBC并发并放入合适大小的连接池中).

(Of course I'm not commenting on the logic of the original question just the responses that imply that concurrency in a scenario with blocking socket IO is possible without the user of a selector pattern - simpler just to work out your typical JDBC concurrency and put in a connection pool of the right size).

看起来 MySql 可能按照我建议的方式做了一些事情 ---http://code.google.com/p/async-mysql-connector/wiki/用法示例

Looks like MySql probably does something along the lines I'm suggesting --- http://code.google.com/p/async-mysql-connector/wiki/UsageExample

这篇关于是否可以进行异步 jdbc 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆