J2EE Transaction Frameworks: Distributed Transaction Primer
Pages: 1, 2, 3, 4
The following example illustrates a stateful session bean that
retains transaction context across three client calls, invoked in the
order {method1, method2, and
method3}.
public class MySessionEJB implements SessionBean {
EJBContext ejbContext;
javax.sql.DataSource ds1;
javax.sql.DataSource ds2;
java.sql.Connection con1;
java.sql.Connection con2;
public void method1(...) {
java.sql.Statement stmt;
InitialContext initCtx = new InitialContext();
// obtain user transaction interface
ut = ejbContext.getUserTransaction();
// start a transaction
ut.begin();
// make some updates on con1
ds1 = (javax.sql.DataSource)
initCtx.lookup("java:comp/env/jdbc/Database1");
con1 = ds1.getConnection();
stmt = con1.createStatement();
stmt.executeUpdate(...);
stmt.executeUpdate(...);
//
// The Container retains the transaction associated with the
// instance to the next client call (which is method2(...)).
}
public void method2(...) {
java.sql.Statement stmt;
InitialContext initCtx = new InitialContext();
// make some updates on con2
ds2 = (javax.sql.DataSource)
initCtx.lookup("java:comp/env/jdbc/Database2");
con2 = ds2.getConnection();
stmt = con2.createStatement();
stmt.executeUpdate(...);
stmt.executeUpdate(...);
// The Container retains the transaction associated with the
// instance to the next client call (which is method3(...)).
}
public void method3(...) {
java.sql.Statement stmt;
// obtain user transaction interface
ut = ejbContext.getUserTransaction();
// make some more updates on con1 and con2
stmt = con1.createStatement();
stmt.executeUpdate(...);
stmt = con2.createStatement();
stmt.executeUpdate(...);
// commit the transaction
ut.commit();
// release connections
stmt.close();
con1.close();
con2.close();
}
...
}
t is possible for an enterprise bean to open and close a database
connection in each business method rather than hold the connection
open until the end of transaction. If the client in the following
example executes the sequence of methods {method1,
method2, method2, method3}, all
the database updates done by the multiple invocations of
method2 are performed in the scope of the same
transaction. This is the transaction started in method1
and committed in method3.
public class MySessionEJB implements SessionBean {
EJBContext ejbContext;
InitialContext initCtx;
public void method1(...) {
java.sql.Statement stmt;
// obtain user transaction interface
ut = ejbContext.getUserTransaction();
// start a transaction
ut.begin();
}
public void method2(...) {
javax.sql.DataSource ds;
java.sql.Connection con;
java.sql.Statement stmt;
// open connection
ds = (javax.sql.DataSource)
initCtx.lookup("java:comp/env/jdbc/Database");
con = ds.getConnection();
// make some updates on con
stmt = con.createStatement();
stmt.executeUpdate(...);
stmt.executeUpdate(...);
// close the connection
stmt.close();
con.close();
}
public void method3(...) {
// obtain user transaction interface
ut = ejbContext.getUserTransaction();
// commit the transaction
ut.commit();
}
...
}
An enterprise bean with bean-managed transaction specification
need not and should not use the getRollbackOnly() and
setRollbackOnly()methods of the EJBContext interface
since, if necessary, it can obtain the status of a transaction by
using the getStatus() method of the
javax.transaction.UserTransaction interface. It can also
roll back a transaction using the rollback()method of the same
interface if required.
Bean Managed transaction for Message Driven Beans
There is never a client transaction context available when a
message driven bean is invoked because a distributed transaction
context does not flow with a JMS message. When a message driven bean
using bean managed transaction uses the
javax.transaction. UserTransaction interface
to specify transactions, the message receipt that causes the
onMessage() method of the bean to be invoked is not part
of the transaction. Container managed transaction with the
Required transaction attribute must be used if receipt of
a message is to be part of a transaction. A message driven bean's
newInstance, setMessageDrivenContext,
ejbCreate, and ejbRemove methods are called
with an unspecified transaction context.
Dibyendu Baksi is a J2EE transactions systems and frameworks designer and developer for Sun Microsystems, Inc.
Return to ONJava.com.