如果我访问UserTransaction这是否意味着我使用2阶段提交或XA? [英] If I access UserTransaction does this mean that I use 2 phase commit or XA?

查看:305
本文介绍了如果我访问UserTransaction这是否意味着我使用2阶段提交或XA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UserTransaction ut = lookup ....
ut.beginTransaction();
saveToFooDB();
statelessEjb.transactionSupportedMethod(); // save something to the Foo DB
saveToFooDB();
ut.commit();



如果我在做上述操作,我的理解是,它不是一个XA事务,因为它不跨越多个资源(如DB加JMS)。

解决方案

数据源可以配置两种类型:




  • XA :这些数据源可以参与分发交易

  • -XA,他们不能参与分布式事务



UserTransaction



然而,实现JTA规范的应用程序服务器可以自由地进行大量的优化。其中之一是 last-agent-optimization ,它允许分布式事务中的最后一个参与者 Local 。然后对最后的参与者进行定期提交。



总之:




  • 如果您有多个参与者,则需要使用XA和2阶段提交

  • 如果只有一个参与者,则大多数应用程序服务器支持本地数据源,并且不使用完整-blow 2阶段提交协议。



对于Glassfish,请参阅:





EDIT b $ b

glassfish的段落transaction scope文档比我更好地解释它。


本地事务只涉及一个
非XA资源,并要求所有应用程序服务器
参与的应用程序组件
在一个进程中执行。 本地
交易优化特定于资源管理器的
,并且对于Java EE
应用程序是透明的



在应用程序服务器中,如果满足以下条件之一,JDBC
资源就是非XA:




  • 在JDBC连接池配置中,DataSource类
    不实现
    javax.sql.XADataSource接口。


  • 未选中全局事务支持框,或者资源
    类型设置不存在或不是
    设置为javax.sql.XADataSource。




如果
以下条件保持为真,则事务仍然是本地的:




  • 仅使用一个非XA资源。如果使用任何其他非XA
    资源,则交易将
    中止。

  • 不发生交易导入或导出。



涉及多个
资源或多个参与者
进程的事务是分布式或全局
事务。如果启用最后
代理优化,全局事务可以
涉及一个非XA资源。
否则,所有资源必须是XA。
默认情况下,use-last-agent-optimization
属性设置为true。
有关如何设置此
属性的详细信息,请参阅配置
Transaction服务。



如果只使用一个XA资源a
事务,发生一阶段提交,
,否则事务是
与两阶段提交
协议协调。



UserTransaction ut=lookup.... ut.beginTransaction(); saveToFooDB(); statelessEjb.transactionSupportedMethod(); //saves something to the Foo DB saveToFooDB(); ut.commit();

If i was doing the above then my understanding is that it is not an XA transaction as it doesn't span across multiple resources (like DB plus JMS). Is my understanding correct?

解决方案

Data source can be configured of two kinds:

  • XA: these datasource can participate in distribute transactions
  • Local: also called non-XA, they can not participate in a distributed transaction

The UserTransaction is defined in the JTA specification which describe how to coordinate the participant in a distributed transaction.

The application server which implements the JTA specification is however free to do a lot of optimizations. One of them is the last-agent-optimization, which allows the last participant in the distributed transaction to be Local. A regular commit is then done for the last participants. If there is only one participant then it's always the case.

In short:

  • if you have more than one participant, XA and 2 phase commit need to be used
  • if there is only one participant, most application server support local data source and do not use the full-blow 2 phase commit protocol.

For Glassfish see:

EDIT

Paragraph "transaction scope" of glassfish documentation explains it better than me. I guess it's the same for all application server.

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.

这篇关于如果我访问UserTransaction这是否意味着我使用2阶段提交或XA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆