自动提交和 Spring 声明式事务

发布于 2024-10-03 17:28:35 字数 338 浏览 0 评论 0原文

我有一个用 Propagation.Required 注释的服务方法。它执行三个独立的操作。

  1. 如果表 1 中没有记录,则从表 z 插入表 1
  2. 根据用户编辑/添加插入/更新表 1
  3. 从表 1 中删除 x 条记录

请原谅我的无知,但所有这些不应该在单个事务下运行吗?从某种意义上说,如果第三个查询遇到异常,第一个查询不应该发生异常吗?第二次回滚也可以吗?我的情况不会发生这种情况。休眠自动提交设置会以任何方式影响 txn 边界吗?在我的例子中,自动提交设置为 true。我要求的是,只有在所有表都成功的情况下,才应在这些表中的任何一个表中进行提交。

I have a single service method annotated with Propagation.Required. It performs three separate operations .

  1. Insert to table 1 from table z if no records are in table 1
  2. Insert/Update table 1 as per user edits/Additions
  3. Delete x records from table 1

Forgive my ignorance but shouldn't all these run under a single transaction ? In the sense, if the third Query runs into an Exception, shouldn't the first & second rollback too ? This doesn't happen in my case. Will hibernate auto commit setting affect the txn boundaries in any way ? Auto commit is set to true in my case. What I require is the commit should take place in any of these tables only if all are successful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

可遇━不可求 2024-10-10 17:28:35

您可以尝试在服务层之上再添加一层并从那里开始事务吗?

could you try to add one more layer higher than service layer and start transaction from there.

落日海湾 2024-10-10 17:28:35

是的,Hibernate connection.autocommit 属性设置将影响事务边界。

如果您将其设置为 true,Hibernate 会将底层 JDBC 连接置于自动提交模式,这会将您执行的每个语句包装在其自己的数据库事务中。

因此,例如,如果您的第三个查询/语句失败,则只有您的第三个查询/语句会回滚。

要将所有三个单元作为一个单元执行,您需要关闭自动提交并在单个事务(声明式或其他形式)的上下文中执行所有三个单元。

Yes, the Hibernate connection.autocommit property setting will affect transaction boundaries.

If you set this to true, Hibernate will put the underlying JDBC connection in autocommit mode, which will wrap each statement you execute in its own database transaction.

So, for example, if your third query/statement fails, only your third query/statement will get rolled back.

To execute all three as a single unit, you need to have autocommit off and execute all three in the context of a single transaction, declarative or otherwise.

染年凉城似染瑾 2024-10-10 17:28:35

你肯定不希望自动提交。这可能会在每次操作后提交。关闭自动提交,并在末尾添加显式提交。

You definitely don't want autocommit on. That will probably commit after every operation. Switch autocommit off, and add an explicit commit at the end.

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