gpt4 book ai didi

java - OracleDataSource.getConnection 是线程安全的吗?

转载 作者:搜寻专家 更新时间:2023-11-01 03:56:41 24 4
gpt4 key购买 nike

我认为找到这个答案会很容易......但不是那么容易。

有人知道 OracleDataSource.getConnection 方法是否线程安全吗?

我指的不是它返回的Connection对象,而是getConnection本身的调用。

具体来说,这个方法:http://download.oracle.com/otn_hosted_doc/jdeveloper/905/jdbc-javadoc/oracle/jdbc/pool/OracleDataSource.html#getConnection()

文档和类没有明确说明,但是,作为一个连接池,我倾向于相信它是。

最佳答案

这是 Java Concurrency in Practice (Brian Goetz) 中讨论的问题:

4.5.1。解释模糊文档

Many Java technology specifications are silent, or at least unforthcoming, about thread safety guarantees and requirements for interfaces such as ServletContext, HttpSession, or DataSource.

...关于 servlet 的信息...

One can make a similar inference about the JDBCDataSource interface, which represents a pool of reusable database connections. A DataSource provides service to an application, and it doesn't make much sense in the context of a single threaded application. It is hard to imagine a use case that doesn't involve calling getConnection from multiple threads. And, as with servlets, the examples in the JDBC specification do not suggest the need for any client side locking in the many code examples using DataSource. So, even though the specification doesn't promise that DataSource is thread-safe or require container vendors to provide a thread-safe implementation, by the same "it would be absurd if it weren't" argument, we have no choice but to assume that DataSource.getConnection does not require additional client-side locking. ...

关于java - OracleDataSource.getConnection 是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26345436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com