SQL Server 事务执行一半出错是否自动回滚整个事务

本文探讨了SQL Server中事务处理的回滚情况。默认情况下,事务出错仅回滚错误语句,而通过SET XACT_ABORT ON或TRY CATCH结构可以实现整个事务的回滚。当客户端网络中断时,未完成的事务也会自动回滚。了解这些机制对于确保数据一致性至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大家都知道SQL Server事务是单个的工作单元。如果某一事务成功,则在该事务中进行的所有数据修改均会提交,成为数据库中的永久组成部分。如果事务遇到错误且必须取消或回滚,则所有数据修改均被清除。

 

所以是不是说事务出错一定会回滚整个事物呢? 先看几个个例子: 

 --createtable 

create table  testrollback(idintprimarykey, namevarchar(10))

 

 SETXACT_ABORTOFF --Default Settings

begin tran

insert into testrollbackvalues(1,'kevin')

insert into testrollbackvalues(2,'kevin')

insert into testrollbackvalues(1,'kevin')

insert into testrollbackvalues(3,'kevin')

commit tran

 

三条成功插入只有第三条语句出错回滚

 

 

--use SETXACT_ABORT ON

SET XACT_ABORTON;

begin tran

insert into testrollbackvalues(1,'kevin')

insert into testrollbackvalues(2,'kevin')

insert into testrollbackvalues(1,'kevin')

insert into testrollbackvalues(3,'kevin')

commit tran

 

select *from testrollback

全部回滚没有数据插入

 

---use trycatch to catch error and rollback whole transcation

begin tran

    begin try

       insert into testrollback values (1,'kevin')

       insert into testrollback values (2,'kevin')

       insert into testrollback values (1,'kevin')

       insert into testrollback values (3,'kevin')

    commit tran

    end try

begin catch

   rollback

end catch

 

全部回滚没有数据插入

 

对于上面的测试可以看到默认情况下SQL Server只是Rollback出错的语句而不是整个事物。所以如果想Rollback整个事物的话可以通过SET XACT_ABORT选项设置或者使用Try Catch之类的捕获错误进行Rollback.

对于出现网络问题会跟上面的结果有点不一样。在执行事务的过程中,如果Client断开,那么SQL Server会自动Rollback整个事物。

SSMS客户端执行第一条语句,去掉commitTran

 SET XACT_ABORTOFF--Default Settings

begin tran

insert into testrollbackvalues(1,'kevin')

insert into testrollbackvalues(2,'kevin')

insert into testrollbackvalues(1,'kevin')

insert into testrollbackvalues(3,'kevin')

 

之后断开连接,在服务器上用DBCC OPENTRAN查看open的事务:

间隔一段时间再执行发现DBCC OPENTRAN已经没有了,而且查询表数据也没有,说明整个事物回滚了。所以在客户端断开且事务没有完成的情况下整个事物回滚。

 

对于上面的测试微软有详细的解释:

If an error prevents the successful completion of a transaction, SQLServer automatically rolls back the transaction and frees all resources held bythe transaction. If the client's network connection to an instance of theDatabase Engine is broken, any outstanding transactions for the connection arerolled back when the network notifies the instance of the break. If the clientapplication fails or if the client computer goes down or is restarted, thisalso breaks the connection, and the instance of the Database Engine rolls backany outstanding connections when the network notifies it of the break. If theclient logs off the application, any outstanding transactions are rolled back.

If a run-time statement error (such as a constraint violation) occurs in abatch, the default behavior in the Database Engine is to roll back only thestatement that generated the error. You can change this behavior using the SETXACT_ABORT statement. After SET XACT_ABORT ON is executed, any run-timestatement error causes an automatic rollback of the current transaction.Compile errors, such as syntax errors, are not affected by SET XACT_ABORT. Formore information, seeSET XACT_ABORT (Transact-SQL)

更多信息参考 ControllingTransactions

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值