根据Hibernate docs,在 JTA 环境中,默认的连接释放模式是 after_statement,这意味着在每条语句之后都会释放 hibernate 逻辑连接。
当逻辑连接被释放时,Connection close() 方法被调用,当前资源被从事务管理器中除名。
根据 RedHat事务开发指南:
"delistResource 方法用于将指定资源与目标对象中的事务上下文分离。应用服务器调用该方法时带有两个参数:
An XAResources object, which represents the resource.
A flag to indicate whether the operation is due to the transaction being suspended (TMSUSPEND), a portion of the work has failed (TMFAIL), or a normal resource release by the application (TMSUCCESS)."
由于 Bitronix 使用 TMSUCCESS:
currentTransaction.delistResource(xaResourceHolderState.getXAResource(), XAResource.TMSUCCESS);
这意味着连接与当前事务分支解除关联,有时您最终可能会为同一个 Resource Adapter 争取 2 个不同的连接。
我认为在事务发生时保持连接是一个更好的选择,因为我们通常在每个事务中执行多个语句。所以 after_transaction 释放模式听起来更有吸引力。
after_transaction 发布模式是否更适合 Bitronix?有人在生产环境中体验过吗?