Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access a database asynchronously through Java NIO non-blocking sockets?

I would like to access a database in a non-blocking way, in other words, I will send the SQL request through a socket and read the query response through the same socket asynchronously. That means that I only read data from the socket as it becomes available (Java NIO SocketChannel) so I never block.

I might be wrong but as far as I know JDBC drivers for databases all use blocking sockets.

Even if I have to execute a query that returns 1 million lines, I can do that asynchronously, receiving lines as they become available in the socket buffer.

My goal is to execute an SQL query without ever blocking (i.e. without ever producing latency). Using a separate thread is not an option. I need to do that inside the network thread (NIO selector thread).

Has anyone succeeded in doing this or can recommend an approach that does not involve an extra thread?

like image 611
Richard Bradley Avatar asked Jan 24 '15 17:01

Richard Bradley


1 Answers

It's possible, but not with JDBC. Unless you want to use the raw SocketChannel interface and parse the results yourself, the only async database drivers on the JVM I'm aware of are https://github.com/mauricio/postgresql-async (which despite the name also supports MySQL). They're written in Scala but it should be possible to call them from Java (since it's a JVM language), though I can't say how Java-friendly the API will be.

like image 175
lmm Avatar answered Sep 19 '22 20:09

lmm