1
0
-1

Dear Expert,

I would like to make a db transaction with the following code.

Connection con = null;
DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
con = ds.getConnection();
try{
	if (!con.isClosed()){
		con.setAutoCommit(false);
		// Some code to select a Record for checking!
		// sql = "select * from tables for update";
		...
		if (checking == true) {
			result = true;
		} else {
			// Some code update another db record here!
			...
        };
		con.commit();
} catch (Exception e) {
	LogUtil.error("Application",e, "Error updateing data");
} finally {
	if (con !=null) {
		con.close();
	};
}

 

However, I get the error of  "java.sql.SQLException: Auto-commit can not be set while enrolled in a transaction"

Everyone can point me how to solve this problem.

 

Many Thanks!

Best Regards

Tony Chu

    CommentAdd your comment...

    1 answer

    1.  
      2
      1
      0

      Ok I found out  the answer and here is the Code by using:

      con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

      and not

      con.setAutoCommit(false);

       

      Connection con = null;
      DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
      con = ds.getConnection();
      try{
          if (!con.isClosed()){
              con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
              // Some code to select a Record for checking!
              // sql = "select * from tables for update";
              ...
              if (checking == true) {
                  result = true;
              } else {
                  // Some code update another db record here!
                  ...
              };
              con.close();
      } catch (Exception e) {
          LogUtil.error("Application",e, "Error updateing data");
      } finally {
          if (con !=null) {
              con.close();
          };
      }
        CommentAdd your comment...