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

2

2

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?

Deirdre answered 10/3, 2010 at 1:29 Comment(0)
H
3

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.

Hellenic answered 11/3, 2010 at 14:15 Comment(1)
In WebSphere world "last-agent-optimization" is known as Last Resource OptimizationDeirdre
M
0

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

Masseur answered 29/7, 2011 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.