[ Team LiB ] Previous Section Next Section

The Java Transaction API and WebLogic

JTA specifies local, high-level Java interfaces between a transaction manager and the other parties involved in a distributed transactional system. The JTA package, javax.transaction, contains three core interfaces:

  • UserTransaction

  • TransactionManager

  • Transaction

The UserTransaction Interface

The UserTransaction interface is implemented for bean-managed transaction boundaries. It defines six methods that allow an application to explicitly manage transaction boundaries:

  • begin— Creates a new transaction associated with the current thread

  • commit— Commits a transaction

  • getStatus— Returns the status of a transaction as an integer

  • rollback— Immediately rolls back the transaction associated with the current thread

  • setRollbackOnly— Does not immediately roll back the transaction, but sets a flag that calls the rollback method after the current operation has completed

  • setTransactionTimeout— Sets the time in seconds in which a transaction associated with the current thread will time out if not completed

    The following code is a high-level example of how to use the UserTransaction API:

    
    
    // create a JNDI Initial context
    Context ctx = new InitialContext();
    // obtain the UserTransaction
    UserTransaction utx = (UserTransaction) ctx.lookup
                                          ("java:comp/UserTransaction");
    // begin the transaction
    utx.begin();
    //  . work.......
    utx.commit();
    

With regular Java applications, such as servlets, RMI applications, JavaBeans, MDBs, and session EJBs, the UserTransaction interface is implemented explicitly in the source code of the application.

However, enterprise components (session beans and MDBs) may use the TX_BEAN_MANAGED transaction attribute, which is set in the bean's deployment descriptor, to implement the UserTransaction interface. In this manner, the application does not directly interface with the transaction manager, but relies on the EJB server to provide support for all of its transaction work. JTA transactions are automatically transferred from the client side to the server side when using RMI (and EJB). So, a server component can participate in a transaction and force it to roll back. However, it cannot commit or roll back; only the participant who started the transaction can finish it.

The TransactionManager Interface

The TransactionManager interface is the interface intended for the application server to manage transaction boundaries on behalf of the application being managed (that is, container-managed transactions). An example of using the TransactionManager interface follows:


Transaction tx = TransactionManager.suspend ();
. . .
TransactionManager.resume (tx);

WebLogic's weblogic.transaction.TransactionManager interface extends the javax.transaction.TransactionManager interface, allowing components using bean-managed transactions to suspend and resume their own transactions.

The Transaction Interface

The Transaction interface is intended for use by a resource manager to participate in and perform operations on the transaction itself. The following code snippet shows how the Transaction interface can be used:


Transaction tx = null;
      TransactionManager tm = getTransactionManager ();
  try
  {
      tx = tm.getTransaction();
  }
  catch(SystemException e)
  {
  }
  if (tx == null)
  {
      try
      {
          tm.begin();
      }
      catch(NotSupportedException e)
      {  }
      catch(SystemException e)
      {  }
  }

A resource manager could use this interface to enlist or delist resources, register for synchronization callbacks, commit or roll back a transaction, and obtain the status of a transaction.

NOTE

WebLogic's weblogic.transaction.Transaction interface extends the javax.transaction.Transaction interface, allowing components using bean-managed transactions to get and set transaction properties.


    [ Team LiB ] Previous Section Next Section