gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:06:43 27 4
gpt4 key购买 nike

我正在尝试了解 SQL Server 中的嵌套事务。让我们考虑以下 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 自治事务的东西,请阅读:Commit transaction outside the current transaction (like autonomous transaction in Oracle)

关于sql - 如果成功提交嵌套事务,父事务是否有可能失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51224400/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com