0

sql server 200 java 1.4 jboss 3

您好收到异常消息“您无法在托管事务期间设置自动提交”

代码如下

    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;

            }

这个异常的原因是什么,它是什么意思?

4

1 回答 1

1

错误很明显,您无法在托管事务中设置自动提交。您甚至不需要将其设置为 false,因为这是默认设置,您可以使用它来启用它自动提交。

我不确定您是否使用 J2EE 和 EJB,如果您使用并且想要启用自动提交,您可以将设置更改为 bean 托管事务 (BMT),这将允许您修改此设置。

但是,您在代码中使用它的方式不需要将其设置为 false,一切都在事务中完成,您可以通过提交或回滚来控制它们。

于 2009-12-19T00:22:10.470 回答