简体   繁体   English

在JTA中关闭jdbc连接

[英]closing jdbc connections in JTA

What will happen when DbConnection.close() is called for a XA Reource? 为XA资源调用DbConnection.close()时会发生什么? Will it be returned to the connection pool or will it be held by the transaction manager until the Global Transaction finishes? 它将返回到连接池中还是由事务管理器保留直到全局事务完成?

I have been cracking my head and looking for so many place for the answer, but none can give me a confirmation. 我一直在努力寻找答案的地方很多,但是没人能给我一个确认。

Most of time, the actual Transaction Manager behavior is vendor-dependent. 大多数时候,事务管理器的实际行为取决于供应商。 But you can refer to the JTA 1.1 Specification too for the standard behavior. 但是您也可以参考JTA 1.1规范以了解标准行为。

  1. Assuming a client invokes an EJB bean with a TX_REQUIRED transaction attribute and the client is not associated with a global transaction, the EJB container starts a global transaction by invoking the TransactionManager.begin method. 假设客户端使用TX_REQUIRED事务属性调用EJB bean,并且该客户端未与全局事务关联,则EJB容器通过调用TransactionManager.begin方法来启动全局事务。
  2. After the the transaction starts, the container invokes the bean method. 事务开始后,容器将调用bean方法。 As part of the business logic, the bean requests for a connection-based resource using the API provided by the resource adapter of interest. 作为业务逻辑的一部分,bean使用感兴趣的资源适配器提供的API请求基于连接的资源。
  3. The application server obtains a resource from the resource adapter via some ResourceFactory.getTransactionalResource method. 应用程序服务器通过某些ResourceFactory.getTransactionalResource方法从资源适配器获取资源。
  4. The resource adapter creates the TransactionalResource object and the associated XAResource and Connection objects. 资源适配器创建TransactionalResource对象以及关联的XAResource和Connection对象。
  5. The application server invokes the getXAResource method. 应用程序服务器调用getXAResource方法。
  6. The application server enlists the resource to the transaction manager. 应用程序服务器将资源注册到事务管理器。
  7. The transaction manager invokes XAResource.start to associate the current transaction to the resource. 事务管理器调用XAResource.start将当前事务与资源关联。
  8. The application server invokes the getConnection method. 应用程序服务器调用getConnection方法。
  9. The application server returns the Connection object reference to the application. 应用程序服务器将Connection对象引用返回给应用程序。
  10. The application performs one or more operations on the connection. 该应用程序对连接执行一项或多项操作。
  11. The application closes the connection. 应用程序关闭连接。
  12. The application server delist the resource when notified by the resource adapter about the connection close. 当资源适配器通知有关连接关闭时,应用程序服务器会取消列出资源。
  13. The transaction manager invokes XAResource.end to disassociate the transaction from the XAResource. 事务管理器调用XAResource.end将事务与XAResource解除关联。
  14. The application server asks the transaction manager to commit the transaction. 应用程序服务器要求事务管理器提交事务。
  15. The transaction manager invokes XAResource.prepare to inform the resource manager to prepare the transaction work for commit. 事务管理器调用XAResource.prepare通知资源管理器准备事务工作以进行提交。
  16. The transaction manager invokes XAResource.commit to commit the transaction. 事务管理器调用XAResource.commit提交事务。

在此处输入图片说明

So to answer you question, while you call the close() , the connection will be delisted, it is somehow still being hold by the Transaction Manager, so you can safely call close() before commit to pre-return the connection to the pool (if you worry about connection leak), although there are lot of Transaction Manager will help to auto-close the connection without you to manually close it (refer to Hibernate framework and Transaction Manager). 因此,为回答您的问题,当您调用close() ,该连接将被除名,事务管理器仍会保留该连接,因此您可以在提交以将连接返回到池之前安全地调用close() (如果您担心连接泄漏),尽管有很多Transaction Manager将有助于自动关闭连接,而无需您手动关闭它(请参阅Hibernate框架和Transaction Manager)。

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

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