简体   繁体   English

如果嵌套事务已成功提交,父事务是否有可能失败

[英]Is it possible for parent transaction to fail if nested transaction was successfully committed

I'm trying to understand nested transactions in SQL Server. 我正在尝试了解SQL Server中的嵌套事务。 Lets consider following chain for SQL commands: 让我们考虑以下SQL命令链:

BEGIN TRANSACTION; -- #1
BEGIN TRANSACTION; -- #2
UPDATE foo SET column = 'something'; -- Change something in one table.
COMMIT TRANSACTION; -- #2

If commit of transaction #2 succeed is it possible for commit of transaction #1 to fail? 如果事务#2的提交成功,事务#1的提交是否可能失败? If yes, could you provide an example when this might happen? 如果是,您可以举一个例子说明何时会发生这种情况吗?

From A SQL Server DBA myth a day: (26/30) nested transactions are real : 每天都有一个SQL Server DBA神话:(26/30)嵌套事务是真实的

The commit of a nested transaction has absolutely no effect – as the only transaction that really exists as far as SQL Server is concerned is the outer one. 嵌套事务的提交绝对没有效果–因为就SQL Server而言,真正存在的唯一事务是外部事务。 ... ...

The rollback of a nested transaction rolls back the entire set of transactions – as there is no such thing as a nested transaction. 嵌套事务的回滚将回滚整个事务集,因为没有嵌套事务之类的东西。

SELECT @@TRANCOUNT;
BEGIN TRANSACTION; -- #1
SELECT @@TRANCOUNT;
BEGIN TRANSACTION; -- #2
SELECT @@TRANCOUNT;
UPDATE foo SET [column] = 'something';
COMMIT TRANSACTION; -- #2
SELECT @@TRANCOUNT;
ROLLBACK;      -- simulate error or explicit rollback
               -- update is lost

DBFiddle Demo DBFiddle演示

If you want something like Oracle autonomous transaction please read: Commit transaction outside the current transaction (like autonomous transaction in Oracle) 如果您需要类似于Oracle自主事务的信息,请阅读: 在当前事务之外提交事务(例如Oracle中的自主事务)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM