Are DB queries initiated from Java always blocking I/O?

blocking, java, multithreading, scheduling

Solution

let's say some blocking I/O is done in Java such as a long running db query. Is there in general a way in Java that some Java database driver can tell the JVM scheduler that the call has left the JVM and is now being processed by some external system?

Uh, no. The whole point of threads is that if they block, a different thread can then be scheduled to take over the processor or other resources. You don't want the JVM somehow using the same thread which holds all of the JDBC state, stack frame, variables, etc.. You do want it using the same processor and other system resources for a different threads' tasks. Remember that a thread has relatively low overhead. On modern systems, you can start running into problems when you have 1000s of them in a JVM mostly because they each have allocated a fixed stack space area.

The way we optimize this as programmers is to use multiple threads, thread-pools, database connection pools, etc.. Then as queries block, other threads and queries can be working in parallel to maximize system throughput.

Problem

let's say some blocking I/O is done in Java such as a long running db query. Is there in general a way in Java that some Java database driver can tell the JVM scheduler that the call has left the JVM and is now being processed by some external system? The JVM could then assign the thread that served the db query for some other operation until the reply from the db has arrived. This way the blocking db query would effectively become non-blocking. Just wonder whether this can be done on the JVM in general. I do Java for many years now, but I admittedly don't know what the Java scheduler is doing in such a situation.

Original source