views:

276

answers:

1

Hi

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  A: 

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.

ewernli