gpt4 book ai didi

java - 关闭JTA中的jdbc连接

转载 作者:行者123 更新时间:2023-11-30 03:15:46 26 4
gpt4 key购买 nike

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

最佳答案

我绞尽脑汁,寻找了很多地方的答案,但没有人能给我确认。

大多数时候,事务管理器的实际行为取决于供应商。但您也可以引用 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.

enter image description here

因此,为了回答您的问题,当您调用 close() 时,连接将被除名,它仍以某种方式被事务管理器保留,因此您可以安全地调用 close () 在提交之前预先将连接返回到池中(如果你担心连接泄漏),虽然有很多事务管理器会帮助自动关闭连接而不需要你手动关闭它(引用Hibernate 框架和事务管理器)。

关于java - 关闭JTA中的jdbc连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32664186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com