ut.beginTransaction();
saveToFooDB();
statelessEjb.transactionSupportedMethod(); //将内容保存到Foo DB
saveToFooDB();
ut.commit();
如果我正在做上述事情,那么我的理解是它不是XA事务,因为它不跨越多个资源(如DB加JMS).我的理解是否正确?
解决方法
> XA:这些数据源可以参与分发事务
> Local:也称为非XA,它们不能参与分布式事务
UserTransaction在JTA规范中定义,描述了如何协调分布式事务中的参与者.
然而,实现JTA规范的应用程序服务器可以自由地进行大量优化.其中之一是last-agent-optimization,它允许分布式事务中的最后一个参与者是Local.然后为最后的参与者进行常规提交.如果只有一个参与者,那么情况总是如此.
简而言之:
>如果您有多个参与者,则需要使用XA和2阶段提交
>如果只有一个参与者,大多数应用程序服务器都支持本地数据源,并且不使用完整的2阶段提交协议.
对于Glassfish,请参阅:
> last-agent-optimization
> configure JDBC data source
编辑
glassfish documentation的“交易范围”段解释得比我好.我猜这对所有应用服务器来说都是一样的.
A local transaction involves only one
non-XA resource and requires that all
participating application components
execute within one process. Local
transaction optimization is specific
to the resource manager and is
transparent to the Java EE
application.In the Application Server,a JDBC
resource is non-XA if it meets any of
the following criteria:
In the JDBC connection pool configuration,the DataSource class
does not implement the
javax.sql.XADataSource interface.The Global Transaction Support Box is not checked,or the Resource
Type setting does not exist or is not
set to javax.sql.XADataSource.A transaction remains local if the
following conditions remain true:
- One and only one non-XA resource is used. If any additional non-XA
resource is used,the transaction is
aborted.- No transaction importing or exporting occurs.
Transactions that involve multiple
resources or multiple participant
processes are distributed or global
transactions. A global transaction can
involve one non-XA resource if last
agent optimization is enabled.
Otherwise,all resourced must be XA.
The use-last-agent-optimization
property is set to true by default.
For details about how to set this
property,see Configuring the
Transaction Service.If only one XA resource is used in a transaction,one-phase commit occurs,otherwise the transaction is coordinated with a two-phase commit protocol.