Search code examples
javatransactionsibatis

Is it safe to use multiple startTransaction and commitTransaction with just one last endTransaction in iBatis?


I wrote a program to insert bulk data. To speed up, I committed transaction in the middle of process. Is it safe to call startTransaction and commitTransaction many times and call endTransaction just once?

try {
  sqlMap.startTransaction();
  // Do some work.      
  sqlMap.commitTransaction();

  sqlMap.startTransaction();
  // Do some work. 
  sqlMap.commitTransaction();

  sqlMap.startTransaction();
  // Do some work. 
  sqlMap.commitTransaction();
} catch (SQLException e) {
  e.printStackTrace();
  throw new MyException();
} finally {
  try {
    sqlMap.endTransaction();
  }
  catch (SQLException e) {
    e.printStackTrace();
    throw new MyException();
  }
}

Thanks.


Solution

  • In iBatis, startTransaction, commitTransaction, endTransaction should be called one time in order as stated. If you call startTransaction and commitTransaction many times without calling endTransaction, you will run out of db connection and get errors.