JDBC concurrency through threads.
Hi Everybody,
Let's assume that I have an application with two (or more threads) and <one> database connection. What will happen if my threads try to execute some databace statements (i.e. update, insert, etc) through the <one and only> database connection at the <same> time? Will this scenario complete succesfully, or should I apply some synchronization technics here?
My question actually concerns Oracle, but I would appreciate a more general answer.
Thank you all for your help,
Kind Regards.
JDBC drivers should be thread safe, but unfortunately as they are stateful there are a number of issues you'll have to contend with.
One common issue is that of handling commits and rollbacks. Obviously if thread A commits, any pending inserts, updates or deletes being done by thread B will be commited. The exact behavior of what happens to result sets in the face of other threads rolling back or commiting is left to the JDBC driver and the underlying database - your mileage may vary.
As for the Oracle drivers (specifically the current thin drivers), I have seen one significant multithreading issue. If two threads attempt to execute separate PreparedStatement's at the same time, the result will be a dead lock within the JDBC driver. The only way around this is to use autocommit mode, but that may not be acceptable.
With care and some degree of coordination, you can have two threads use a single JDBC connection concurrently. What you can get away with and how much coordination you need is largely dependent on the JDBC driver.
Chuck