Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > Develop Transactions > Develop components to use transactions


Use component-managed transactions

We can enable a session bean, servlet, or application client component to use component-managed transactions, to manage its own transactions directly instead of letting the container manage the transactions.

Entity beans cannot manage transactions (so cannot use bean-managed transactions).

To enable a session bean, servlet, or application client component to use component-managed transactions...


Procedure

  1. For session beans, set the Transaction type attribute in the component deployment descriptor to Bean, as described in Configure transactional deployment attributes.

  2. For application client components, enable support for transaction demarcation by setting the Allow JTA Demarcation attribute in the component deployment descriptor, as described in Configure transactional deployment attributes.
  3. Write the component code to actively manage transactions.

    For stateful session beans, a transaction started in a given method does not have to be completed (that is, committed or rolled back) before completing that method. The transaction can be completed at a later time, for example on a subsequent call to the same method, or even within a different method. However, it is usually preferable to construct the application so that a transaction is begun and completed within the same method call, because it simplifies application debugging and maintenance.

    The following code extract shows the standard code required to obtain an object encapsulating the transaction context, and involves the following steps:

    • Create a javax.transaction.UserTransaction object by calling a lookup on java:comp/UserTransaction.

    • Use the UserTransaction object to demarcate the boundary of a transaction by using transaction methods such as begin and commit, as needed. If an application component begins a transaction, it must also complete that transaction by invoking either the commit method or the rollback method.

    Code example: Getting an object that encapsulates a transaction context

    ...
    import javax.transaction.*;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    ...
        public float doSomething(long arg1)throws NamingException {
            InitialContext initCtx = new InitialContext();
            UserTransaction userTran = (UserTransaction)initCtx.lookup(
               "java:comp/UserTransaction");
            ...
            //Use userTran object to call transaction methods
            userTran.begin ();
            //Do transactional work
            ...
            userTran.commit ();
            ...
        }
        ...
    }
    


Client support for transactions
Use the transaction service
Configure transactional deployment attributes
Develop components to use transactions


Related


Local transaction containment

+

Search Tips   |   Advanced Search