简体   繁体   English

如何重置 JDBC 连接对象?

[英]How to reset a JDBC Connection object?

How to reset a JDBC Connection object (that is, a java.sql.Connection object)?如何重置 JDBC Connection 对象(即 java.sql.Connection 对象)?

I have explored the concept of connection pooling.我已经探索了连接池的概念。 When a connection pool is used, a Connection object can be recycled.当使用连接池时,可以回收一个 Connection 对象。 But how can a connection pool recycle a Connection object?但是连接池如何回收一个 Connection 对象呢? I think that a connection needs to be "reset" (for example, if it is in a transaction, then perhaps rollback it, but there may be more things to reset) before it can be reused.我认为连接需要被“重置”(例如,如果它在一个事务中,那么可能会回滚它,但可能有更多的东西要重置)才能被重用。 But I cannot find such a "reset" method in the Java documentation about the class java.sql.Connection.但是我在关于类 java.sql.Connection 的 Java 文档中找不到这样的“重置”方法。

If you are talking about what you as a user of a connection should do, then that is simple: call close() on the connection.如果您正在谈论作为连接用户应该做什么,那么这很简单:在连接上调用close() Closing the logical connection will signal to the connection pool that the connection is available for reuse, and the connection pooling manager is then responsible for performing the necessary reset, invalidation of statement handles, etc.关闭逻辑连接将向连接池发出信号,表明该连接可以重用,然后连接池管理器负责执行必要的重置、语句句柄的失效等。

If you are talking about what you as the implementer of a connection pooling manager should do, that is where things become complicated.如果您正在谈论作为连接池管理器的实现者应该做什么,那么事情就变得复杂了。

Historically, JDBC provides no way to 'reset' a java.sql.Connection , other than by your own code (or your third-party connection pool) remembering the initial configuration, and restoring it after use, and keeping track of objects like statements and result sets and closing them when the connection is returned to the pool.从历史上看,JDBC 没有提供“重置” java.sql.Connection方法,只能通过您自己的代码(或您的第三方连接池)记住初始配置,并在使用后恢复它,并跟踪语句等对象和结果集并在连接返回到池时关闭它们。

Originally, the intended way to reset connections in JDBC was for an application server to use a driver's javax.sql.ConnectionPoolDataSource as a factory for javax.sql.PooledConnection objects.最初,在 JDBC 中重置连接的预期方法是让应用程序服务器使用驱动程序的javax.sql.ConnectionPoolDataSource作为javax.sql.PooledConnection对象的工厂。 These PooledConnection objects serve as handles for physical connections to be held in a connection pool (to be clear, ConnectionPoolDataSource is not a connection pool, it is a data source for a connection pool).这些PooledConnection对象作为在连接池举行,物理连接手柄(要清楚, ConnectionPoolDataSource是不是一个连接池,它一个连接池数据源)。 The application server would then expose a javax.sql.DataSource handing out logical Connection objects, where on Connection.close() , the driver specific implementation of PooledConnection would take care of any necessary reset of a connection (though JDBC underspecifies what is 'necessary').然后,应用服务器将公开一个javax.sql.DataSource分发逻辑Connection对象,其中在Connection.close()PooledConnection的驱动程序特定实现将负责任何必要的连接重置(尽管 JDBC 未指定什么是“必要的”) ')。

However, in practice this route is hardly ever used because support in drivers was (and often still is) spotty, inconsistent or downright incorrect, and JDBC wasn't clear enough on exactly what needed to be done when the logical connection was closed.然而,在实践中这条路线几乎从未被使用过,因为驱动程序的支持是(而且通常仍然是)参差不齐、不一致或完全不正确的,并且 JDBC 对关闭逻辑连接时需要做什么还不够清楚。 The world has also shifted to using third-party connection pool libraries that do not use ConnectionPoolDataSource .世界也转向使用不使用ConnectionPoolDataSource的第三方连接池库。

JDBC 4.3 (Java 9 and higher) introduced the methods Connection.beginRequest() and Connection.endRequest() to be called by connection pooling managers, which would seem to fit such pattern, but unfortunately JDBC 4.3 doesn't actually specify what kind of things an implementation should or should not do in response to beginRequest and endRequest . JDBC 4.3(Java 9 及更高版本)引入了由连接池管理器调用的Connection.beginRequest()Connection.endRequest()方法,这似乎符合这种模式,但不幸的是 JDBC 4.3 实际上并没有指定哪种类型实现应该或不应该响应beginRequestendRequest

In short, there is no real general way to reset a connection in JDBC.简而言之,在 JDBC 中没有真正通用的方法来重置连接。

Do you try to close() method and reinitialize Connection object again.您是否尝试关闭()方法并再次重新初始化 Connection 对象。

close() Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released close() 立即释放此 Connection 对象的数据库和 JDBC 资源,而不是等待它们自动释放

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

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