开发者

Connection.close vs. PooledConnection.close

开发者 https://www.devze.com 2023-04-11 21:54 出处:网络
I\'m reading Java Data Access—JDBC, JNDI, and JAXP about Connection, PooledConnection interfaces. As I understand Connection returned by PooledConnection represents a \"logical\" connection. PooledCo

I'm reading Java Data Access—JDBC, JNDI, and JAXP about Connection, PooledConnection interfaces. As I understand Connection returned by PooledConnection represents a "logical" connection. PooledConnection - itself represents "physical". What's the difference between them concerning closing?

Calling the close() method on a standard Connection object closes the physical connection. In contrast, calling the close() method on a logical Connection object returns the logical connection to the pool for other clients to use.

  1. My first question is: Should I invoke 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 开发者_如何学Cpool.

  2. The second question: I don't see why I need to call Connection.close() on the object, returned by PooledConnection.getConnection()

  3. If you look at the example: PooledConnection example only Connection.close() method is invoked, but not PooledConnection.close(). Please comment it.

  4. According to the official documentation: PooledConnection.close PooledConnection actually closes the physical connection that this PooledConnection object represents. 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. Could you comment this also.

Thanks.


PooledConnection is typically only used by containers directly, and is used to manage connection pools. 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. 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.


You don't have the choice. All you have is an object of some class that implements Connection, and it has a close() method. So call it. No decision necessary. 'An application programmer does not use the PooledConnection interface directly'.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号