2009-12-16 39 views
0

SQL服務器200 的Java 1.4 JBoss的3java:無法在java 1.4 api中設置值爲false的自動提交模式?

HI我

代碼如下

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; 

      } 

這個是什麼原因 「有管理的交易過程中,不能設置自動提交」 獲得異常消息 例外,這是什麼意思?

回答

1

錯誤很明顯,您無法在託管事務中設置自動提交。您甚至不需要將其設置爲false,因爲這是默認設置,您可以使用它來啓用它自動提交。

我不確定您是否使用J2EE和EJB,如果您是並且您希望啓用自動提交,則可以將您的設置更改爲Bean管理事務(BMT),這將允許您修改此設置。

但是,您在代碼中使用它的方式不需要將其設置爲false,所有操作都在事務中完成,並且您可以使用commit或rollback來控制它們。

相關問題