UserTransaction Support in EJB Server
EJB servers are required to support the UserTransaction interface for use by EJB beans with the BEAN value in the javax.ejb.TransactionManagement
annotation (this is called bean-managed transactions or BMT). The UserTransaction interface is exposed to EJB components through either the EJBContext interface using the getUserTransaction method, or directly via injection using the general @Resource annotation. Thus, an EJB application does not interface with the Transaction Manager directly for transaction demarcation; instead, the EJB bean relies on the EJB server to provide support for all of its transaction work as defined in the Enterprise JavaBeans Specification. (The underlying interaction between the EJB Server and the TM is transparent to the application; the burden of implementing transaction management is on the EJB container and server provider.)
The code sample below illustrates the usage of UserTransaction via bean-managed transactions in an EJB session bean:
@Stateless @TransactionManagement(BEAN) public class ExampleBean { @Resource private UserTransaction utx; public void foo { // start a transaction utx.begin; // Do work // Commit it utx.commit; } }Alternatively, the UserTransaction can be obtained from the SessionContext:
@Stateless @TransactionManagement(BEAN) public class ExampleBean { @Resource private SessionContext ctx; public void foo { UserTransaction utx = ctx.getUserTransaction; // start a transaction utx.begin; // Do work // Commit it utx.commit; } }Note though that in the example above if the @TransactionManagement(BEAN) annotation is omitted, a JTA transaction is automatically started whenever foo is called and is automatically committed or rolled back when foo is exited. Making use of a UserTransaction is thus not necessary in EJB programming, but might be needed for very specialized code.
Read more about this topic: Java Transaction API
Famous quotes containing the word support:
“Have a sense of piety ever on your mind, and be ever mindful that this is subject to no change, but will last you as long as life and support you in death. Elevate your soul by prayer and by contemplation without mystical enthusiasm.”
—James Boswell (17401795)