Question

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?

Was it helpful?

Solution

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 autocommit, 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 your code you don't need to set it to false, everything is done in transactions and you control them with commit or rollback.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top