Is asynchronous jdbc call possible?

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

Leave a Comment