【问题标题】:java:not able to set auto commit mode with value false in java 1.4 api?java:无法在 java 1.4 api 中设置值为 false 的自动提交模式?
【发布时间】:2009-12-16 06:49:59
【问题描述】:

sql 服务器 200 爪哇 1.4 老板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;

            }

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

【问题讨论】:

    标签: sql-server transactions rollback java1.4 autocommit


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 2016-04-04
      • 1970-01-01
      相关资源
      最近更新 更多