递归/嵌套事务
我有2个存储过程A,B,都在一个事务块中。问题是 A 调用 B,因此存在递归/嵌套事务。我不确定是否有问题?当我打电话给 A 时会发生什么?
引用自《MySQL存储过程编程》:
START TRANSACTION
表示新事务的开始。如果现有事务已在进行中,则START TRANSACTION
将发出隐式COMMIT
。当您发出START TRANSACTION
时,自动提交属性(将在下一节中描述)将有效且隐式地设置为 0,直到事务结束。我们建议您在任何START TRANSACTION
语句之前显式提交或回滚现有事务,因为隐式COMMIT
对于阅读或维护您的代码的人来说可能并不明显。
I have 2 store procedures A, B, both are in a transaction block. The problems is A calls B, so, there is recursive/nested transaction. I'm not sure is there problem? What will happen when I call A?
Cited from "MySQL Stored Procedure Programming":
START TRANSACTION
signifies the commencement of a new transaction. If an existing transaction is already in progress, thenSTART TRANSACTION
will issue an implicitCOMMIT
. When you issueSTART TRANSACTION
, the autocommit property (described in the next section) is effectively and implicitly set to 0 until the transaction ends. We recommend that you explicitly commit or roll back existing transactions before anySTART TRANSACTION
statements, since the implicitCOMMIT
might not be obvious to someone reading or maintaining your code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果两个过程都在同一资源上设置/使用锁,那么您将遇到死锁。
B
持有A
也需要的某个表/行/字段的锁,这意味着A
无法获得自己的锁。因此,A
将等待获取锁,直到 DBMS 超时并回滚。If both procedures set/use locks on the same resource, then you'll get a deadlock.
B
holds a lock on some table/row/field thatA
also requires, meaningA
can't get its own lock. SoA
will sit and wait to acquire the lock until the DBMS times it out and rolls things back.只要 B 不也调用 A,就不会出现递归情况,只有嵌套事务 - 这应该没问题。
As long as B doesn't also call A, you don't have a recursive situation, just a nested transaction - which should be fine.
是的,这是可能的。它们只是不能重叠。您可以使用 SAVEPOINT 来完成此操作。
请参阅此答案:
事务中的 Mysql 事务
Yes, it is possible. They just cannot overlap. You can do it using SAVEPOINT.
See this answer:
Mysql transactions within transactions