Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is javax.sql.DataSource thread safe?

I am using the PostgreSQL 9.1 JDBC4 driver (postgresql-9.1-902.jdbc4.jar) in a Java EE application deployed in JBoss 7.

Can I assume that javax.sql.DataSource is thread-safe so that multiple threads can concurrently call the getConnection() method on it?

like image 200
Marcus Junius Brutus Avatar asked Feb 14 '13 10:02

Marcus Junius Brutus


People also ask

Is Java SQL connection thread-safe?

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

Is JDBC DataSource thread-safe?

In Version 5 Release 1, JTOpen 2.0, or later: Yes. All public Toolbox JDBC objects are threadsafe and can be shared between multiple threads.

Are database connections thread-safe?

Database connections are not thread-safe. That's why ActiveRecord uses a separate database connection for each thread. These three connections will remain connected to the database server after the threads terminate. This only affects threads that use ActiveRecord.

What is thread-safe programming?

Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ensures that all threads behave properly and fulfill their design specifications without unintended interaction.


1 Answers

javax.sql.DataSource itself is an interface, so it is a specific to the implentation if it is thread-safe or not.

For the postgres sql driver, I recommend that you read Chapter 10. Using the Driver in a Multithreaded or a Servlet Environment from the official documentation:

The PostgreSQL JDBC driver is thread safe. [...]

like image 108
Dag Avatar answered Oct 05 '22 00:10

Dag