2011-11-07 53 views
0

我有一個問題,使用iBatis作爲Web應用程序的數據庫框架。我想在幾次插入之後手動提交事務,但iBatis會在每次插入後自動提交。我怎樣才能防止呢?如何停止iBatis提交插入,更新和刪除?

這裏是我的SqlMapConfig.xml文件內容:

<sqlMapConfig> 

<settings enhancementEnabled="true" 
    errorTracingEnabled="true" 
    useStatementNamespaces="false" /> 

<transactionManager type="JDBC" commitRequired="false" > 
    <dataSource type="JNDI"> 
     <property name="DataSource" 
      value="java:/comp/env/jdbc/MY_DB" /> 
    </dataSource> 
</transactionManager> 

<sqlMap resource="com/my/common/Common.xml" /> 

</sqlMapConfig> 

回答

2

我也在學習ibatis的/ MyBatis的逐漸讓我抓住了進去。我不能告訴你關於sqlMapConfig的所有不同屬性,因爲我對某些設置不確定,但是我想讓你在單個事務中包含幾個插入。與批量更新類似,將批次包裝在單個事務中。這個例子是基於iBatis的2.3.4

try { 
     sqlMap.startTransaction(); 
     sqlMap.startBatch(); 
     for (final ObjectXXXDTO objectReference1 : GenericObjectList) { 
      sqlMap.insert("createExample1", objectReference1); 
     } 
     sqlMap.insert("createExample2", otherReference2); 
     sqlMap.insert("createExample3", otherReference3); 
     sqlMap.executeBatch(); 
     sqlMap.commitTransaction(); 
    } catch (final SQLException e) { 
     throw new XXXException(e); 
    } finally { 
     try { 
      sqlMap.endTransaction(); 
     } catch (SQLException e) { 
      throw new XXXException(e); 
     } 
    } 

但是請注意,只要您使用的是批量集生成的密鑰數據庫將不會生成報表,直到您叫executeBatch()方法。這意味着如果您使用selectKey更新您的對象與生成的鍵,他們將返回null。如果您有任何需要將新生成的密鑰作爲另一個插入的一部分的對象,則可以在startBatch()之前進行插入或更新。

再次我不確定這是否是您的方法,但我想過發佈它。謝謝

+1

這個想法很好,它的工作原理,但它不是我所需要的。不管怎麼說,還是要謝謝你。 – radonys

1

元素的「commitRequired」屬性是你所需要的。

The <transactionManager> element also allows an optional attribute commitRequired that can be true or 
false. Normally iBATIS will not commit transactions unless an insert, update, or delete operation has been 
performed. This is true even if you explicitly call the commitTransaction() method. This behavior 
creates problems in some cases. 

myBatis guide

另外,我建議你在行動閱讀iBatis的。