SqlTransaction 和嵌套事务
是否可以使用 SqlTransaction 类创建嵌套事务?如果是这样,我需要注意哪些规则/限制?
Is it possible to create nested transactions with a the SqlTransaction class? If so, what are the rules/limitations that I need to be aware of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,SQL Server 忽略内部事务的提交,如这篇 MSDN 文章中所示。
您可以嵌套事务,但请注意该行为可能不是您所期望的。
在最外层事务提交之前不会提交任何内容。
因此,在下面...
查询 B 的结果实际上并未提交到数据库。
Unfortunately SQL Server ignores commits on the inner transactions, as in this MSDN article.
You can nest transactions, but be aware the behavior may not be what you expect.
Nothing is committed until the outermost transaction commits.
So, in the following...
The result of Query B is not actually committed to the database.
您可以在 SqlTransaction 类上使用 Save(string savePointName) 方法。这将创建一个保存点,您可以在事务内回滚到该保存点。因此,如果部分代码失败,您将回滚到之前的保存点并重新开始。
示例:
当你失败时,你会这样做:
你可以按照自己的意愿多次这样做。这将解决您的嵌套事务问题。
You can use
Save(string savePointName)
method on the SqlTransaction class. This will create a save point to which you can rollback inside the transaction. So if part of your code fails, you will rollback to the previous save point and start over.Example:
and when you fail you will do:
You can do this as many times you want. That will solve your problem of nested transactions.
是的,可以创建嵌套事务。
例如:
在这个序列中,只有动作 1、4 和 5 会实际发生。据我所知,除了事务本身的限制之外,嵌套事务没有任何限制。唯一必须理解的是,在提交“顶级”(嵌套的顶部)事务之前,实际上没有提交任何内容。
Yes it's possible to create nested transactions.
For instance:
with this sequence, only actions 1, 4, and 5 will have actually happened. Nested transactions have no limitations other than the limitations of a transaction by itself that I'm aware of. The only thing that must be understood is that nothing is actually committed until the "top level" (the top of the nest) transaction is committed.