12.11.2014 Views

web server - Borland Technical Publications

web server - Borland Technical Publications

web server - Borland Technical Publications

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Programmatic transaction management using JTA APIs<br />

Under normal circumstances only two attributes, Required and RequiresNew, must be<br />

used. The attributes Supports and NotSupported are strictly for optimization. The use of<br />

Never and Mandatory are not recommended because they affect the composibility of the<br />

bean. In addition, if a bean is concerned about transaction synchronization and<br />

implements the javax.ejb.SessionSynchronization interface, then the Assembler/<br />

Deployer can specify only the attributes Required, RequiresNew, or Mandatory. These<br />

attributes ensure that the container invokes the bean only within a global transaction,<br />

because transaction synchronization can only occur within a global transaction.<br />

Programmatic transaction management using JTA APIs<br />

All transactions use the Java Transaction API (JTA). When transactions are container<br />

managed, the platform handles the demarcation of transaction boundaries and the<br />

container uses the JTA API; you do not need to use this API in your bean code.<br />

A bean that manages its own transactions (bean-managed transaction), however, must<br />

use the JTA javax.transaction.UserTransaction interface. This interface allows a client<br />

or component to demarcate transaction boundaries. Enterprise JavaBeans that use<br />

bean-managed transactions use the method EJBContext.getUserTransaction().<br />

In addition, all transactional clients use JNDI to look up the UserTransaction interface.<br />

This simply involves constructing a JNDI InitialContext using the JNDI naming<br />

service, as shown in the following line of code:<br />

javax.naming.Context context = new javax.naming.InitialContext();<br />

Once the bean has obtained the InitialContext object, it can then use the JNDI<br />

lookup() operation to obtain the UserTransaction interface, as shown in the following<br />

code sample.<br />

javax.transaction.UserTransaction utx = (javax.transaction.UserTransaction)<br />

context.lookup("java:comp/UserTransaction");<br />

Note that an EJB can obtain a reference to the UserTransaction interface from the<br />

EJBContext object. This is because an enterprise bean by default inherits a reference to<br />

the EJBContext object. Thus, the bean can simply use the<br />

EJBContext.getUserTransaction() method rather than having to obtain an<br />

InitialContext object and then using the JNDI lookup() method. However, a<br />

transactional client that is not an enterprise bean must use the JNDI lookup approach.<br />

When the bean or client has the reference to the UserTransaction interface, it can then<br />

initiate its own transactions and manage these transactions. That is, you can use the<br />

UserTransaction interface methods to begin and commit (or rollback) transactions. You<br />

use the begin() method to start the transaction, then the commit() method to commit the<br />

changes to the database. Or, you use the rollback() method to abort all changes<br />

made within the transaction and restore the database to the state it was in prior to the<br />

start of the transaction. Between the begin() and commit() methods, you include code<br />

to carry out the transaction's business.<br />

Chapter 19: Transaction management 177

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!