is javax.sql.DataSource thread safe?

▼魔方 西西 提交于 2019-11-28 10:04:06

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. [...]

Ian Roberts

Typically the DataSource implementation you get from a Java EE container will be a thread-safe object backed by a connection pool, and thread-safety (or otherwise) of the underlying JDBC connections is not really relevant. The usual pattern when you need to talk to the database is to call getConnection() on the data source to obtain a connection object, make the necessary database calls and then close() the connection. Under the covers this won't actually close the underlying connection, but simply return it to the connection pool for future use. Any individual connection will only be used by one thread at a time.

This is the idiom used by things like the Spring JdbcTemplate.

If it's a 'Connection pooling implementation', then it should be thread safe.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!