dcsimg
Can we enable a transaction across multiple database connections using JTA
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

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

Re: Can we enable a transaction across multiple database connections using JTA

Posted By:   Nick_Maiorano  
Posted On:   Tuesday, February 4, 2003 08:07 PM

Hi,



Absolutely not. You cannot maintain a transaction if you close the connection. This is true regardless of the setAutoCommit(). The only solution to you problem is to refactor your code. Have you considered using entity beans? This may be your solution.

About | Sitemap | Contact