views:

370

answers:

1

sql server 200 java 1.4 jboss 3

HI am getting exception message "You cannot set autocommit during a managed transaction"

code is below

    try {
    try {
     connection = getConnection();
    } catch (Exception e) {
     throw new ConnectionException(e.getMessage());
    }
    for(int i=0;i<recordIds.size();i++)
    {
     String currentRecordId=(String)recordIds.get(i);
     try
     {
     //exception on this line connection.setAutoCommit(false);
      preparedStatement = connection.prepareStatement(getSQL("PurgeRecordInDumpData"));  
      preparedStatement.setLong(1,Long.parseLong(currentRecordId));
      int numberOfUpdates=preparedStatement.executeUpdate();
      if(numberOfUpdates!=1)
      {
       throw new Exception("Record with record id "+currentRecordId +"could not be purged.");
      }
      preparedStatement.close();
      connection.commit();
      listOfPurgedRecords.add(currentRecordId);
     }
     catch(Exception e)
     {
      connection.rollback();
     }
    }
    return listOfPurgedRecords;

   }

what is cause of this exception and what does it mean?

+1  A: 

The error is clear, you cannot set autocommit while you are in a managed transaction. You should not even need to set this to false as that is the default, you use to enable it autocommit.

I am not sure if you are using J2EE and EJB's, if you are and you want to ENABLE autocomment, you can change your setting to bean managed transaction (BMT) and this would allow you to modify this setting.

However the way you are using it in you code you don't need to set it to false, everything is done in transactions and you control them with commit or rollback.

Rodney Foley