如何阻止 iBatis 提交插入、更新和删除?

发布于 2024-12-14 00:21:42 字数 608 浏览 2 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2024-12-21 00:21:42

我也在学习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() 之前进行此插入或更新。

我再次不确定这是否是您所追求的方法,但我还是想发布它。谢谢

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

梦回梦里 2024-12-21 00:21:42

该元素的“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.

<一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.

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

Also, I would recommend you to read iBatis in Action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文