The transaction management contract handles transactions between the application and the EIS. It has two entities. The transaction manager controls and coordinates transactions across multiple resource managers. The resource manager is the resource adapter of its underlying EIS.
A resource manager can provide three levels of transaction support:
To support local transactions, a resource adapter must implement the javax.resource.spi.LocalTransaction interface. If the resource adapter supports the CCI, it will also send a number of transaction events to the application server. The application server must implement the javax.resource.spi.ConnectionEventListener interface, which allows the application server to receive and handle the following local transaction events:
By listening for these events, the application server can perform various functions such as a local transaction cleanup.
In a managed environment, the application server does the following to support the transaction manager:
By using the managed environment, the programmer does not need to think about managing the transaction because the application server uses the transaction manager to provide this service.
Global transactions use the two-phase commit. If a resource adapter implements XAResource and supports global transactions, it must also support one-phase commits. This allows the transaction manager to optimize one-phase commits.
A two-phase commit involves the following actions:
An optimization that can be implemented is to force the uses of a one-phase commit when there is only a single resource manager referenced in the transaction. Therefore, the transaction does not require the additional overhead of a two-phase commit. In this case, the transaction manager skips phase one and goes directly to phase two.
Another type of optimization, called last resource or last agent optimization, enables the use of a single one-phase commit resource in a global transaction along with any number of two-phase commit resources. In phase one, the transaction manager sends the prepare message to the two-phase commit resources. If the transaction manager receives positive responses, the transaction manager first sends the commit message to the one-phase commit resource followed by the commit message to the two-phase commit resources.
As an added benefit, this optimization allows a resource adapter that does not implement the XAResource interface to participate in a global transaction.
Note that last resource optimization is outside of the scope of the J2EE Connection Architecture specification and a particular application server may or may not support it.