Distributed Transactions   «Prev  Next»

Lesson 7 Bean-managed transactions
Objective Program bean-managed transactions

Beans may manage their own Transactions

Beans can manage their own transactions directly by communicating with the transaction service. The container provides access to the transaction manager through the UserTransaction getUserTransaction() method of javax.ejb.EJBContext. UserTransaction is in the javax.transaction package. The sample code below illustrates the retrieval of the UserTransaction object:

SessionContext sc =  ? // initialized when bean is created
...
javax.transaction.UserTransaction t = sc.getUserTransaction();

J2EE javadoc documentation

Use the J2EE javadoc documentation to view the UserTransaction object details in your browser.

The methods of UserTransaction are as follows:
begin() Create a new transaction and associate it with the current thread
void commit() Complete the transaction associated with the current thread
int getStatus() Obtain the status of the transaction associated with the current thread
void rollback() Roll back the transaction associated with the current thread
void setRollbackOnly() Modify the transaction associated with the current thread such that the only possible outcome of the transaction is toroll back
void setTransactionTimeout(int seconds) Modify the value of the timeout that is associated with the transactions started by the current thread in the begin method

Transaction exceptions

The exceptions that can be thrown by these methods are as follows (all extend java.rmi.RemoteException):
  1. HeuristicCommitException
  2. HeuristicMixedException
  3. HeuristicRollbackException
  4. InvalidTransactionException
  5. NotSupportedException
  6. RollbackException
  7. SystemException
  8. TransactionRequiredException
  9. TransactionRolledbackException
For a more complete understanding of these exceptions, refer to the javax.transaction.documentation.

Class constants

The class constants that indicate the status of the transaction are as follows:
  1. STATUS_ACTIVE
  2. STATUS_COMMITTED
  3. STATUS_MARKED_ROLLBACK
  4. STATUS_NO_TRANSACTION
  5. STATUS_PREPARED
  6. STATUS_PREPARING
  7. STATUS_ROLLEDBACK
  8. STATUS_ROLLING_BACK
  9. STATUS_UNKNOWN
These constants are returned from the getStatus() method and indicate the status of the current transaction. Because transactions cannot be nested, an object only can partake in a single transaction at a time. getStatus() returns the current status of the current transaction.
The following diagram contains Bean managed Transactions and the session context method.
Bean managed Transaction
  1. Using the SessionContext method getUserTransaction(), program gets UserTransaction object stores its reference in ut
  2. This line verifies that there is no current transaction context using getStatus() method. The return is compared to the STATUS_NO_TRANSACTION class constant. If there is a transaction, the program throws a CustomerException.
  3. Begin a transaction within a try-catch block.
  4. The actual code to update the state of one or more recoverable resources would be inserted here
  5. If there was any problem that was discovered then the transaction will be rolled back.
  6. The transaction manager was asked to commit the transaction

Click the following link to view the Bean managed Transactions

J2EE documentation

Remember to look up any of the classes and methods in the javadoc j2ee documentation.
In the next lesson container-managed transactions will be discussed.