Posted By:
Karthikeyan_Balakrishnan
Posted On:
Sunday, January 26, 2003 07:45 PM
I am calling multiple business logic methods which individually opens db connection and closes it before the end of the method. I ahve to call a bunch of these methods inside a transation. i use JTA and started a user transaction . Within the begin() and commit() statements i have embedded these methods, The sample code is given below. Can i maintain transaction between multiple connections 'open-close' condition. Do we have to set the connection setAutoCommit(false); suggest me public class MySessionEJB implements SessionBean { EJBContext ejbContext; InitialContext initCtx; public void doTransaction(...) { java.sql.Statement stmt; // obtain user transaction interface ut = ejbContext.getUserT
More>>
I am calling multiple business logic methods which individually opens db connection and closes it before the end of the method. I ahve to call a bunch of these methods inside a transation. i use JTA and started a user transaction . Within the begin() and commit() statements i have embedded these methods, The sample code is given below. Can i maintain transaction between multiple connections 'open-close' condition. Do we have to set the connection setAutoCommit(false); suggest me
public class MySessionEJB implements SessionBean {
EJBContext ejbContext;
InitialContext initCtx;
public void doTransaction(...) {
java.sql.Statement stmt;
// obtain user transaction interface
ut = ejbContext.getUserTransaction();
// start a transaction
ut.begin();
method1();
method2();
method3();
ut.commit();
}catch(Exception e){ ut.rollback();}
}
public void method1(...) {
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 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(...) {
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();
}
...
}
<<Less