简体   繁体   English

Connection.close与PooledConnection.close

[英]Connection.close vs. PooledConnection.close

I'm reading Java Data Access—JDBC, JNDI, and JAXP about Connection, PooledConnection interfaces. 我正在阅读有关Connection, PooledConnection接口的Java Data Access—JDBC, JNDI, and JAXP Connection, PooledConnection Java Data Access—JDBC, JNDI, and JAXP As I understand Connection returned by PooledConnection represents a "logical" connection. 据我了解Connection通过返回PooledConnection代表“逻辑”连接。 PooledConnection - itself represents "physical". PooledConnection本身表示“物理”。 What's the difference between them concerning closing? 他们在结账方面有什么区别?

Calling the close() method on a standard Connection object closes the physical connection. 在标准Connection对象上调用close()方法将关闭物理连接。 In contrast, calling the close() method on a logical Connection object returns the logical connection to the pool for other clients to use. 相反,在逻辑Connection对象上调用close()方法会将逻辑连接返回到池,以供其他客户端使用。

  1. My first question is: Should I invoke PooledConnection.close()? 我的第一个问题是:我应该调用PooledConnection.close()吗? If i'm right, I don't want physical connection closing, I only want to check the physical connection out (to release it) to its pool. 如果我是对的,我不想关闭物理连接,我只想将物理连接检出(释放)到其池中。

  2. The second question: I don't see why I need to call Connection.close() on the object, returned by PooledConnection.getConnection() 第二个问题:我不明白为什么我需要调用Connection.close()的对象,通过对返回PooledConnection.getConnection()

  3. If you look at the example: PooledConnection example only Connection.close() method is invoked, but not PooledConnection.close() . 如果查看示例: PooledConnection示例,仅调用Connection.close()方法,而不调用PooledConnection.close() Please comment it. 请发表评论。

  4. According to the official documentation: PooledConnection.close PooledConnection actually closes the physical connection that this PooledConnection object represents. 根据官方文档: PooledConnection.close PooledConnection实际上关闭此PooledConnection对象表示的物理连接。 But as I understand connection pool main (one of main) purpose - keep physical connections, without closing/creating it. 但是据我了解,连接池的主要目的(主要目的之一)-保持物理连接,而无需关闭/创建连接。 According to documentation if I run PooledConnection.close() then connection pool will have to recreate it again. 根据文档,如果我运行PooledConnection.close(),则连接池将不得不再次重新创建它。 Could you comment this also. 您能否也对此发表评论。

Thanks. 谢谢。

PooledConnection is typically only used by containers directly, and is used to manage connection pools. PooledConnection通常仅由容器直接使用,并用于管理连接池。 You only need to deal with these if you are writing a container or something that will give out connections to other code/libraries that will do the actual work. 仅在编写容器或将提供与其他将执行实际工作的代码/库的连接的连接时,您才需要处理这些问题。

Standard java.sql.Connection objects are used by JDBC clients, and are used for actually doing database work. 标准java.sql.Connection对象由JDBC客户端使用,并用于实际执行数据库工作。 This code should not be touching PooledConnection, and will either be given connections to it by a container (such as a Java EE container or via dependency injection) or will create connections itself from a DataSource or from DriverManager. 该代码不应触及PooledConnection,并且将通过容器(例如Java EE容器或通过依赖项注入)为其提供连接,或者将自己从DataSource或DriverManager创建连接。

You don't have the choice. 您别无选择。 All you have is an object of some class that implements Connection, and it has a close() method. 您所拥有的只是实现Connection的某个类的对象,并且它具有close()方法。 So call it. 所以叫它。 No decision necessary. 无需决定。 'An application programmer does not use the PooledConnection interface directly'. “应用程序程序员不直接使用PooledConnection接口”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM