Chapter 41. Handling Transactions from JOnAS EJB Clients

This chapter is for the application developer intending to initiate transactions (that will be managed by the EJB Server) from the client side.

41.1. Starting a Transaction from the Client

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

Each one can be 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 Section 14.2 Bean-managed Transactions. 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();