是异步的JDBC调用可能吗? [英] Is asynchronous jdbc call possible?

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

问题描述

我不知道是否有一种方法,使异步调用到数据库?

举例来说,假设我有这需要很长的时间来处理一个大的要求,我想发送请求并收到通知时,该请求将返回一个值(通过传递一个监听器/回调或东西) 。我不想阻止等待数据库来回答。

我不认为使用线程池是一个解决方案,因为它不结垢,重并发请求的情况下,这将产生一个非常大的数量的线程。

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

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

** **更新结果
由于缺乏真正的实际的解决方案,我决定建立一个库(欺骗的一部分)自己: finagle- MySQL的。它基本上去codeS /德codeS mysql的请求/响应,而发动机罩下使用欺骗/ Netty的。它甚至有连接的数量庞大的尺度非常好。


解决方案

我不明白任何包装在JDBC调用演员提出的办法,遗嘱执行人或其他任何东西可以帮助在这里 - 有人可以澄清。

当然,基本的问题是,JDBC操作上的插座IO阻塞。这样做时它会阻止它的线程上运行 - 故事的结尾。无论包装框架您选择使用它要与一个线程最终会被不停地忙碌着/每阻塞并发请求。

如果底层的数据库驱动程序(MySQL的?)提供的拦截套接字创建的一种手段(见的SocketFactory),那么我想这将有可能建立在JDBC API之上的异步事件驱动的数据库层,但我们不得不封装整个JDBC事件驱动的门面后面,而且门面不会像JDBC(这将是事件驱动后)。数据库处理会发生异步在不同的线程调用者,你就必须制定出如何构建不依赖于线程关联的事务管理器。

喜欢的东西我提到将允许甚至单个后台线程来处理并发JDBC Exec的的负载的方法。在实践中你可能运行的线程池来充分利用多核的。

(当然我不评论对原来问题的逻辑只是意味着一个场景,并发阻塞与插孔IO的响应可能是没有选择器模式的用户 - 简单的只是制定出典型的JDBC并发性和放在一个大小合适的连接池)。


看起来像MySQL的大概会沿着我建议线的东西---
<一href=\"http://$c$c.google.com/p/async-mysql-connector/wiki/UsageExample\">http://$c$c.google.com/p/async-mysql-connector/wiki/UsageExample

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.

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?

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).

** 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.

解决方案

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.

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.

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.

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.

(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).


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天全站免登陆