If I access UserTransaction does this mean that I use 2 phase commit or XA?

℡╲_俬逩灬. 提交于 2019-12-18 13:47:30

问题


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?


回答1:


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:

  • last-agent-optimization
  • configure JDBC data source

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.




回答2:


Once you start the UserTransaction, and then obtain a connection to the resource (eg databases) using a connection-factory which is declared to be xa-supportive, it means that connection will become part of the XA transaction. Also, it does not matter at all whether you are connecting to single or multiple types of resources like JMS and database.

Hope that helps.

Nitin



来源:https://stackoverflow.com/questions/2413911/if-i-access-usertransaction-does-this-mean-that-i-use-2-phase-commit-or-xa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!