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

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

我正在尝试了解 SQL 服务器中的嵌套事务。让我们考虑以下 SQL 命令的链:

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

如果事务#2 的提交成功,事务#1 的提交是否有可能失败?如果是,您能否提供一个可能发生的示例?

来自A SQL Server DBA myth a day: (26/30) nested transactions are real

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

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

如果你想要类似 Oracle 自治事务的东西,请阅读: