如何阻止 iBatis 提交插入、更新和删除?
我在使用 iBatis 作为 Web 应用程序的数据库框架时遇到问题。我想在几次插入后手动提交事务,但 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也在学习Ibatis/MyBatis并逐渐掌握它。我无法告诉您 sqlMapConfig 的所有不同属性,因为我不确定某些设置,但我认为您希望在单个事务中包含多个插入?与批量更新类似,将批量包装在单个事务中。此示例基于 IBatis 2.3.4
,但请注意,无论何时使用批处理语句集,在调用
executeBatch()
方法之前,数据库生成的键都不会生成。这意味着如果您使用selectKey
使用生成的键更新对象,它们将返回 null。如果您有任何对象需要新生成的密钥作为另一个插入的一部分,那么您可以在startBatch()
之前进行此插入或更新。我再次不确定这是否是您所追求的方法,但我还是想发布它。谢谢
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
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 usingselectKey
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 thestartBatch()
.Again I'm uncertain if this is the approach you are after but I thought of posting it anyways. Thanks
该元素的“commitRequired”属性就是您所需要的。
<一href="http://www.google.ru/url?sa=t&rct=j&q=mybatis%20commitrequired%20attribute&source=web&cd=3&ved=0CC4QFjAC&url=https://mybatis 。粘性物glecode.com/files/MyBatis-SqlMaps-2_en.pdf&ei=YxO9TsfEJciUOoWcwL0B&usg= AFQjCHoaItIw2fBt5dXkL2BML7M6WB89w&sig2=UfKWYb9PH3o6Gx22rEEIbg&cad=rja" rel="nofollow">myBatis 指南
另外,我建议您阅读《iBatis in Action》。
the "commitRequired" attribute of the element is what you need.
myBatis guide
Also, I would recommend you to read iBatis in Action.