Handling Transactions from JOnAS EJB Clients

  1. Target Audience and Rationale
  2. Starting a Transaction from the Client

Target Audience and Rationale

The target audience for this guide is the application developer intending to initiate transactions (that will be managed by the EJB Server) from the client side.

Starting a Transaction from the Client

Depending on the architecture of the application, the client may be

each one communicating with one or several EJB servers via RMI.

The way to initiate a transaction from a client application is to obtain a UserTransaction object (an instance of javax.Transaction.UserTransaction), that is provided by the application server. This UserTransaction object provides methods for starting, committing and rollbacking transactions.

The way to obtain the UserTransaction object if the client is an Enterprise Bean is defined in the Transactional Behaviour section of the Bean Programmer's Guide. This consists in calling the EJBContext.getUserTransaction() method or to use JNDI to retrieve UserTransaction with the name "java:comp/UserTransaction" in the initial context.

If the client is not an Enterprise Bean, as JOnAS does not provide web containers neither client containers, the way to retrieve the UserTransaction object is JOnAS specific, and consists in performing a JNDI lookup with the name "javax.Transaction.UserTransaction".

To start and close a transaction from the client application:

UserTransaction utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction"); 
utx.begin();
...                // code of the transaction
utx.commit();      // or utx.rollback();