9.5.2. Lifecycle of a JTA Transaction
When a resource asks to participate in a transaction, a chain of events is set in motion. The Transaction Manager is a process that lives within the application server and manages transactions. Transaction participants are objects which participate in a transaction. Resources are datasources, JMS connection factories, or other JCA connections.
Your application starts a new transaction
To begin a transaction, your application obtains an instance of classUserTransaction
from JNDI or, if it is an EJB, from an annotation. TheUserTransaction
interface includes methods for beginning, committing, and rolling back top-level transactions. Newly-created transactions are automatically associated with their invoking thread. Nested transactions are not supported in JTA, so all transactions are top-level transactions.CallingUserTransaction
.begin()
starts a new transaction. Any resource that is used after that point is associated with the transaction. If more than one resource is enlisted, your transaction becomes an XA transaction, and participates in the two-phase commit protocol at commit time.Your application modifies its state.
In the next step, your transaction performs its work and makes changes to its state.Your application decides to commit or roll back
When your application has finished changing its state, it decides whether to commit or roll back. It calls the appropriate method. It callsUserTransaction
.commit()
orUserTransaction
.rollback()
. This is when the two-phase commit protocol (2PC) happens if you have enlisted more than one resource. Section 9.2.9, “About the 2-Phase Commit Protocol”The transaction manager removes the transaction from its records.
After the commit or rollback completes, the transaction manager cleans up its records and removes information about your transaction.
Failure recovery
Failure recovery happens automatically. If a resource, transaction participant, or the application server become unavailable, the Transaction Manager handles recovery when the underlying failure is resolved.