user manual

108 BES Developers Guide
}
...
When finished with the session bean handle, the client can remove it with the
javax.ejb.EJBHome.remove(Handle handle) method.
Managing transactions
A client program can manage its own transactions rather than letting the
enterprise bean (or container) manage the transaction. A client that manages
its own transaction does so in exactly the same manner as a session bean
than manages its own transaction.
When a client manages its own transactions, it is responsible for delimiting the
transaction boundaries. That is, it must explicitly start the transaction and end
(commit or roll back) the transaction.
A client uses the javax.transaction.UserTransaction interface to manage its own
transactions. It must first obtain a reference to the UserTransaction interface,
using JNDI to do so. Once it has the UserTransaction context, the client uses
the UserTransaction.begin() method to start the transaction, followed later by
the UserTransaction.commit() method to commit and end the transaction (or
UserTransaction.rollback() to rollback and end the transaction). In between,
the client does its queries and updates.
This code sample shows the code that a client would implement to manage its
own transactions. The parts that pertain specifically to client-managed
transactions are highlighted in bold.
...
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
...
public class clientTransaction {
public static void main (String[] argv) {
UserTransaction ut = null;
InitialContext initContext = new InitialContext();
...
ut = (UserTransaction)initContext.lookup("java:comp/
UserTransaction");
// start a transaction
ut.begin();
// do some transaction work
...
// commit or rollback the transaction
ut.commit(); // or ut.rollback();
...
]
]