SQL Server 事务执行一半出错是否自动回滚整个事务
2013-08-08 16:48
453 查看
大家都知道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
所以是不是说事务出错一定会回滚整个事物呢? 先看几个个例子:
--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
相关文章推荐
- SQL Server 事务执行一半出错是否自动回滚整个事务 推荐
- SQL Server事务执行一半出错是否自动回滚整个事务 【转】
- 使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在方法调用之前决定是否开启一个事务,并在方法执行之后决定事务提交或回滚事务。
- SET XACT_ABORT { ON | OFF }SQL Server 是否自动回滚到当前事务
- SET XACT_ABORT { ON | OFF }SQL Server 是否自动回滚到当前事务
- SQL技巧:如何获取存储过程用return返回的值与判断SQL Server事务是否执行成功?
- SQL Server 出错自动回滚
- SQL Server 出错自动回滚
- SQL Server 事务自动回滚
- 使用事务自动回滚来实现单元测试
- oracle_存储过程_没有参数_根据配置自动创建申请单以及写日志事务回滚
- sql server 自动执行存储过程
- 自动提交&事务&回滚
- SQL SERVER 2000 嵌套 存储过程 事务 回滚
- SQL Server 2008维护计划 出错 无法实现自动备份
- JDBC对事务的使用(包括自动提交,回滚等知识)
- 启动SQL SERVER时自动执行存储过程
- SQLServer陷阱(二) 运行时错误与自动回滚事务
- sql server里执行delete或者update操作产生大量事务日志,导致空间不够,执行失败,能不能设置此类动作时不生成事务日志的
- SQL SERVER 事务执行情况跟踪分析