The Java Transaction API includes a javax.transaction.Synchronization interface, which issues notifications before and after a transaction is completed. Objects implementing this interface can be registered with a Transaction object. Just before the transaction’s completion process begins, the TransactionManager calls the Synchronization object’s beforeCompletion() method. After the transaction is committed or rolled back, the TransactionManager calls the Synchronization object’s afterCompletion() method.

The beforeCompletion() method is usually used to perform any last-minute work. For example, an application might use this callback to write some built-up state to the database.

The afterCompletion() method is called after the commit or rollback, and passes in a status code indicating which of those outcomes occurred. Applications can use this callback to clean up any state or resources that were used during the transaction.

To register synchronizations directly, your code must use the TransactionManager to get a hold of the Transaction object. J2EE components do not have explicit access to the TransactionManager interface, so J2EE provides other ways for its components to receive synchronization callbacks. Specifically, stateful session EJBs can implement the javax.ejb.SessionSynchronization interface, which includes methods for receiving synchronization notifications.

 
loading table of contents...