Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Enterprise JavaBeans™ Specification, Version 2.0 - Sun Microsystems.pdf
Скачиваний:
14
Добавлен:
24.05.2014
Размер:
2.71 Mб
Скачать

Sun Microsystems Inc.

Support for Transactions

Enterprise JavaBeans 2.0, Public Draft

Bean Provider’s responsibilities

Additional care must be taken if multiple enterprise beans access the same resource manager in the same transaction. Conflicts in the requested isolation levels must be avoided.

16.4.3 Enterprise beans using bean-managed transaction demarcation

This subsection describes the requirements for the Bean Provider of an enterprise bean with bean-man- aged transaction demarcation.

The enterprise bean with bean-managed transaction demarcation must be a Session bean or a Mes- sage-driven bean.

An instance that starts a transaction must complete the transaction before it starts a new transaction.

The Bean Provider uses the UserTransaction interface to demarcate transactions. All updates to the resource managers between the UserTransaction.begin() and UserTransaction.commit() methods are performed in a transaction. While an instance is in a transaction, the instance must not attempt to use the resource-manager specific transaction demarcation API (e.g. it must not invoke the commit() or rollback() method on the java.sql.Connection interface or on the javax.jms.Session interface).

A stateful Session Bean instance may, but is not required to, commit a started transaction before a business method returns. If a transaction has not been completed by the end of a business method, the Container retains the association between the transaction and the instance across multiple client calls until the instance eventually completes the transaction.

The bean-managed transaction demarcation programming model presented to the programmer of a stateful Session Bean is natural because it is the same as that used by a stand-alone Java application.

A stateless session bean instance must commit a transaction before a business method returns.

A message-driven bean instance must commit a transaction before the onMessage method returns.

5/31/00

310

Sun Microsystems Inc

Bean Provider’s responsibilities

Enterprise JavaBeans 2.0, Public Draft

Support for Transactions

The following example illustrates a business method that performs a transaction involving two database connections.

public class MySessionEJB implements SessionBean { EJBContext ejbContext;

public void someMethod(...) { javax.transaction.UserTransaction ut; javax.sql.DataSource ds1; javax.sql.DataSource ds2; java.sql.Connection con1; java.sql.Connection con2; java.sql.Statement stmt1; java.sql.Statement stmt2;

InitialContext initCtx = new InitialContext();

// obtain con1 object and set it up for transactions ds1 = (javax.sql.DataSource)

initCtx.lookup(“java:comp/env/jdbc/Database1”); con1 = ds1.getConnection();

stmt1 = con1.createStatement();

// obtain con2 object and set it up for transactions ds2 = (javax.sql.DataSource)

initCtx.lookup(“java:comp/env/jdbc/Database2”); con2 = ds2.getConnection();

stmt2 = con2.createStatement();

//

//Now do a transaction that involves con1 and con2.

ut = ejbContext.getUserTransaction();

//start the transaction

ut.begin();

//Do some updates to both con1 and con2. The Container

//automatically enlists con1 and con2 with the transaction. stmt1.executeQuery(...);

stmt1.executeUpdate(...); stmt2.executeQuery(...); stmt2.executeUpdate(...); stmt1.executeUpdate(...); stmt2.executeUpdate(...);

//commit the transaction

ut.commit();

// release connections stmt1.close(); stmt2.close(); con1.close(); con2.close();

}

...

}

311

5/31/00

Sun Microsystems Inc.

Support for Transactions

Enterprise JavaBeans 2.0, Public Draft

Bean Provider’s responsibilities

The following example illustrates a business method that performs a transaction involving both a database connection and a JMS connection.

public class MySessionEJB implements SessionBean { EJBContext ejbContext;

public void someMethod(...) { javax.transaction.UserTransaction ut; javax.sql.DataSource ds; java.sql.Connection dcon; java.sql.Statement stmt; javax.jms.QueueConnectionFactory qcf; javax.jms.QueueConnection qcon; javax.jms.Queue q; javax.jms.QueueSession qsession; javax.jms.QueueSender qsender; javax.jms.Message message;

InitialContext initCtx = new InitialContext();

// obtain db conn object and set it up for transactions

ds = (javax.sql.DataSource) initCtx.lookup(“java:comp/env/jdbc/Database”);

dcon = ds.getConnection();

stmt = dcon.createStatement();

// obtain jms conn object and set up session for transactions qcf = (javax.jms.QueueConnectionFactory)

initCtx.lookup(“java:comp/env/jms/qConnFactory”); qcon = qcf.createQueueConnection();

qsession = qcon.createQueueSession(true,0); q = (javax.jms.Queue)

initCtx.lookup(“java:comp/env/jms/jmsQueue”); qsender = qsession.createSender(q);

message = qsession.createTextMessage(); message.setText(“some message”);

//

//Now do a transaction that involves the two connections.

ut = ejbContext.getUserTransaction();

//start the transaction

ut.begin();

//Do database updates and send message. The Container

//automatically enlists dcon and qsession with the

//transaction.

stmt.executeQuery(...); stmt.executeUpdate(...); stmt.executeUpdate(...); qsender.send(message);

// commit the transaction ut.commit();

5/31/00

312

Sun Microsystems Inc

Bean Provider’s responsibilities

Enterprise JavaBeans 2.0, Public Draft

Support for Transactions

// release connections stmt.close(); qsender.close(); qsession.close(); dcon.close(); qcon.close();

}

...

}

313

5/31/00

Sun Microsystems Inc.

Support for Transactions

Enterprise JavaBeans 2.0, Public Draft

Bean Provider’s responsibilities

The following example illustrates a stateful Session Bean that retains a transaction across three client calls, invoked in the following 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();

5/31/00

314

Sun Microsystems Inc

Bean Provider’s responsibilities

Enterprise JavaBeans 2.0, Public Draft

Support for Transactions

stmt.executeUpdate(...);

stmt = con2.createStatement(); stmt.executeUpdate(...);

//commit the transaction ut.commit();

//release connections stmt.close(); con1.close(); con2.close();

}

...

}

315

5/31/00