Using Bean-Managed Transactions

   

Using Client-Demarcated Transactions

You've seen client transactions referenced throughout this chapter without much mention of how they're started and completed. When you're using container-managed demarcation , the container transparently starts a transaction if a bean method needs one and the client hasn't provided it. If you choose bean-managed demarcation, you can start a transaction using the getUserTransaction method of EJBContext and the begin method of the UserTransaction . These cases handle the situations where a call to a bean method coincides with the start of a transaction or where a transaction context is received from another EJB. What hasn't been covered is the situation where the client providing the transaction context isn't an EJB.

The J2EE platform requires that a JSP or servlet be able to obtain a UserTransaction using the following JNDI lookup:

 Context ctx = new InitialContext();  UserTransaction tx = (UserTransaction)ctx.lookup("java:comp/UserTransaction"); 

Using this approach, a Web tier client can be responsible for transaction demarcation. Once a client obtains a UserTransaction , it can call begin to start a transaction, access a database (or any other XAResource ) or an EJB, and then commit the transaction or roll it back. One limitation is that a transaction must be fully contained within a single Web request. The most important consideration about managing a transaction this way is that you really shouldn't be doing this in a multi-tier application. Unless you have a resource that is for some reason only associated with the Web tier, transactional processing belongs in the application tier. You should place any transaction needed by a Web client within a session bean method to keep the separation of responsibilities clear. This also avoids any possibility of a transaction being started by the Web tier that is committed only after some response by the end user is received. JSP or servlet access to a JTA transaction is only appropriate for two-tier applications where the business logic resides in the Web tier.

Caution

Although the specification plainly states that you should be able to obtain a transaction using a JNDI lookup on java:comp/UserTransaction , you can't always rely on this. Some vendors might require you to use another environment entry name , such as javax.transaction.UserTransaction .


Although J2EE requires that Web tier components have access to a JTA transaction, the same isn't true for client Java applications and applets. A particular application server might provide this access, but it's not guaranteed to be supported by others. As with the Web tier, client applications and applets should leave all responsibility for transaction management to enterprise beans.



Special Edition Using Enterprise JavaBeans 2.0
Special Edition Using Enterprise JavaBeans 2.0
ISBN: 0789725673
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net