java.sql.Connection线程安全吗? [英] Is java.sql.Connection thread safe?
本文介绍了java.sql.Connection线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
重新解释一下这个问题:我应该避免在不同的线程之间共享实现 java.sql.Connection
的类的实例吗?
To rephrase the question: should I avoid sharing instances of classes which implement java.sql.Connection
between different threads?
推荐答案
如果JDBC驱动程序符合规范,那么从技术上讲,该对象是线程安全的,但是应该避免在线程之间共享连接,因为连接上的活动将是意味着一次只有一个线程可以执行任何操作。
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.
您应该使用连接池(如 Apache Commons DBCP )确保每个线程都有自己的连接。
You should use a connection pool (like Apache Commons DBCP) to ensure that each thread gets its own connection.
这篇关于java.sql.Connection线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文