javamultithreadingjdbcthread-safetydbconnection

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?


Solution

  • 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.