Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java.sql.Connection thread safe?

To rephrase the question: should I avoid sharing instances of classes which implement java.sql.Connection between different threads?

like image 204
Boris Pavlović Avatar asked Oct 07 '09 11:10

Boris Pavlović


People also ask

Is Java sql Date thread safe?

sql objects be multi-thread safe and able to cope correctly with having several threads simultaneously calling the same object."

Is JDBC Connection object thread safe?

The PostgreSQL™ JDBC driver is thread safe. Consequently, if your application uses multiple threads then you do not have to worry about complex algorithms to ensure that only one thread uses the database at a time.

Is Oracle JDBC connection thread safe?

Oracle states the thread safety of its Connection implementation in its official document: The Oracle JDBC drivers provide full support for, and are highly optimized for, applications that use Java multithreading … However, Oracle strongly discourages sharing a database connection among multiple threads.

Is thread safe in Java?

Since String is immutable in Java, it's inherently thread-safe. 2) Read-only or final variables in Java are also thread-safe in Java. 3) Locking is one way of achieving thread-safety in Java. 4) Static variables if not synchronized properly become a major cause of thread-safety issues.


4 Answers

If the JDBC driver is spec-compliant, then technically yes, the object is thread-safe, but you should avoid sharing connections between threads, since the activity on the connection will mean that only one thread will be able to do anything at a time.

You should use a connection pool (like Apache Commons DBCP) to ensure that each thread gets its own connection.

like image 191
skaffman Avatar answered Oct 09 '22 09:10

skaffman


java.sql.Connection is an interface. So, it all depends on the driver's implementation, but in general you should avoid sharing the same connection between different threads and use connection pools. Also it is also advised to have number of connections in the pool higher than number of worker threads.

like image 25
Andrey Adamovich Avatar answered Oct 09 '22 08:10

Andrey Adamovich


This is rather an old thread, but for those who are looking for an answer regarding Microsoft SQL Server, here is the answer:

SQLServerConnection is not thread safe, however multiple statements created from a single connection can be processing simultaneously in concurrent threads.

and

SQLServerConnection implements a JDBC connection to SQL Server.

From all the above, you can share statements but not Connections, and in case you need a connection in each thread, you may use a thread pool.

Read more here

like image 4
Hanash Yaslem Avatar answered Oct 09 '22 09:10

Hanash Yaslem


Oracle JDBC and Multithreading docs:

Because all Oracle JDBC API methods are synchronized, if two threads try to use the connection object simultaneously, then one will be forced to wait until the other one finishes its use.

So it may be safe in Oracle case but concurrent access would suffer from bottleneck.

like image 4
Vadzim Avatar answered Oct 09 '22 10:10

Vadzim