非同期JDBC呼び出しは可能ですか? 質問する

非同期JDBC呼び出しは可能ですか? 質問する

データベースへの非同期呼び出しを行う方法はあるのでしょうか?

たとえば、処理に非常に長い時間がかかる大きなリクエストがあるとします。リクエストを送信し、リクエストが値を返すときに通知を受け取りたいとします (リスナー/コールバックなどを渡すことによって)。データベースが応答するのを待ってブロックしたくはありません。

スレッド プールの使用は、スケーラブルではないため、解決策ではないと思います。同時リクエストが大量に発生すると、非常に多くのスレッドが生成されます。

私たちはネットワーク サーバーでこの種の問題に直面しており、select/poll/epoll システム コールを使用して接続ごとに 1 つのスレッドを回避するという解決策を見つけました。データベース要求で同様の機能を実現するにはどうしたらよいか知りたいのですが。

注: FixedThreadPool を使用するのが良い回避策になるかもしれないことは承知していますが、誰も本当に非同期のシステム (追加のスレッドを使用せずに) を開発していないことに驚いています。

** 更新 **
実際の実用的なソリューションがないため、自分でライブラリ (finagle の一部) を作成することにしました。フィナグル-mysql基本的に、mysql のリクエスト/レスポンスをデコード/デコードし、内部で Finagle/Netty を使用します。接続数が非常に多い場合でも、非常に優れた拡張性を発揮します。

ベストアンサー1

JDBC 呼び出しをアクター、エグゼキューター、またはその他のものでラップする提案されたアプローチがここでどのように役立つのか理解できません。誰か説明してもらえますか。

確かに、基本的な問題は、JDBC 操作がソケット IO でブロックされることです。これが発生すると、実行中のスレッドがブロックされます。これで終わりです。どのラッピング フレームワークを使用するか選択しても、同時要求ごとに 1 つのスレッドがビジー状態/ブロック状態になります。

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

おすすめ記事