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?
SQLServerConnection is not thread-safe, however multiple statements created from a single connection can be processing simultaneously in concurrent threads.
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.
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.
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.
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. [...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With