I have a problem using iBatis as database framework for a web application. I want to manually commit the transaction after a couple of inserts, but iBatis auto commits it after every insert. How can I prevent that?
Here is my SqlMapConfig.xml file content:
<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>
I'm also learning Ibatis/MyBatis and gradually getting my grips into it. I cannot tell you about all the different attributes of the sqlMapConfig as I am uncertain on some of the settings but I take you want several inserts to be included in a single transaction? Similar to a batch update, wrap the batch in a single transaction. This example is based on 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);
}
}
However note that whenever you are using a batched set of statements the database generated keys will not be generated until you have called the executeBatch()
method. This means if you are using selectKey
to update your objects with the generated keys they will return null. If you have any object that requires the newly generated key as part of another insert then you can have this insert or update before the startBatch()
.
Again I'm uncertain if this is the approach you are after but I thought of posting it anyways. Thanks