Is java.sql.Connection thread safe?

Boris Pavlović picture Boris Pavlović · Oct 7, 2009 · Viewed 46.1k times · Source

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

Answer

skaffman picture skaffman · Oct 7, 2009

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.