Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
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

It 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). In the following example, if the client executes the sequence of methods (method1, method2, method2, method2, and method3), all the database updates done by the multiple invocations of method2 are performed in the scope of the same transaction, which 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();

}

...

}

16.4.3.1 getRollbackOnly() and setRollbackOnly() method

An enterprise bean with bean-managed transaction demarcation must not use the getRollbackOnly() and setRollbackOnly() methods of the EJBContext interface.

5/31/00

316