在 JTA 中关闭 jdbc 个连接

closing jdbc connections in JTA

当为 XA 资源调用 DbConnection.close() 时会发生什么?它会返回到连接池还是由事务管理器持有直到全局事务完成?

我一直在绞尽脑汁寻找答案,但是none可以给我一个确认。

大多数时候,实际的事务管理器行为取决于供应商。但是您也可以参考 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.
  2. After the the transaction starts, the container invokes the bean method. As part of the business logic, the bean requests for a connection-based resource using the API provided by the resource adapter of interest.
  3. The application server obtains a resource from the resource adapter via some ResourceFactory.getTransactionalResource method.
  4. The resource adapter creates the TransactionalResource object and the associated XAResource and Connection objects.
  5. The application server invokes the getXAResource method.
  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.
  8. The application server invokes the getConnection method.
  9. The application server returns the Connection object reference to the application.
  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.
  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.
  16. The transaction manager invokes XAResource.commit to commit the transaction.

所以为了回答你的问题,当你调用 close() 时,连接将被删除,它仍然以某种方式被事务管理器持有,所以你可以在提交之前安全地调用 close()预 return 到池的连接(如果你担心连接泄漏),尽管有很多事务管理器将帮助自动关闭连接而无需你手动关闭它(参考 Hibernate 框架和事务经理)。