java.sql.Connection 线程安全吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1531073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:53:23  来源:igfitidea点击:

Is java.sql.Connection thread safe?

javamultithreadingjdbcthread-safetydbconnection

提问by Boris Pavlovi?

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

重新表述这个问题:我应该避免共享java.sql.Connection在不同线程之间实现的类的实例吗?

采纳答案by skaffman

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.

如果 JDBC 驱动程序符合规范,那么技术上是的,该对象是线程安全的,但您应该避免在线程之间共享连接,因为连接上的活动意味着一次只有一个线程能够执行任何操作.

You should use a connection pool (like Apache Commons DBCP) to ensure that each thread gets its own connection.

您应该使用连接池(如Apache Commons DBCP)来确保每个线程都有自己的连接。

回答by Andrey Adamovich

java.sql.Connection is an interface. So, it all depends on the driver's implementation, but in general you should avoid sharing the same connection between different threads and use connection pools. Also it is also advised to have number of connections in the pool higher than number of worker threads.

java.sql.Connection 是一个接口。因此,这一切都取决于驱动程序的实现,但通常您应该避免在不同线程之间共享相同的连接并使用连接池。此外,还建议池中的连接数高于工作线程数。

回答by Mirak

We had ArrayOutOfBoundsException on the Websphere statement cache of it's pooleddatasource, and we had to disable that cache.

我们在其池化数据源的 Websphere 语句缓存上有 ArrayOutOfBoundsException,我们不得不禁用该缓存。

We had a treatment that was blocking itself.

我们接受了一种自我阻断的治疗。

All of that because of current access to the connection, so the conclusion by real life practice, is that you must not do that.

所有这一切都是因为当前对连接的访问​​,所以通过现实生活实践得出的结论是,你不能那样做。

回答by Hanash Yaslem

This is rather an old thread, but for those who are looking for an answer regarding Microsoft SQL Server, here is the answer:

这是一个相当古老的线程,但对于那些正在寻找有关 Microsoft SQL Server 的答案的人,这里是答案:

SQLServerConnection is not thread safe, however multiple statements created from a single connection can be processing simultaneously in concurrent threads.

SQLServerConnection 不是线程安全的,但是从单个连接创建的多个语句可以在并发线程中同时处理。

and

SQLServerConnection implements a JDBC connection to SQL Server.

SQLServerConnection 实现到 SQL Server 的 JDBC 连接。

From all the above, you can share statements but not Connections, and in case you need a connection in each thread, you may use a thread pool.

综上所述,您可以共享语句但不能共享连接,并且如果您需要在每个线程中都有一个连接,您可以使用线程池。

Read more here

在这里阅读更多

回答by Vadzim

Oracle JDBC and Multithreadingdocs:

Oracle JDBC 和多线程文档:

Because all Oracle JDBC API methods are synchronized, if two threads try to use the connection object simultaneously, then one will be forced to wait until the other one finishes its use.

因为所有 Oracle JDBC API 方法都是同步的,如果两个线程尝试同时使用连接对象,那么一个将被迫等待,直到另一个使用完毕。

So it may be safe in Oracle case but concurrent access would suffer from bottleneck.

所以在 Oracle 情况下可能是安全的,但并发访问会遇到瓶颈。