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

上传人:野鹰 文档编号:3174164 上传时间:2017-07-31 格式:DOCX 页数:3 大小:110.26KB
返回 下载 相关 举报
SQL Server 事务执行一半出错是否自动回滚整个事务_第1页
第1页 / 共3页
SQL Server 事务执行一半出错是否自动回滚整个事务_第2页
第2页 / 共3页
SQL Server 事务执行一半出错是否自动回滚整个事务_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《SQL Server 事务执行一半出错是否自动回滚整个事务》由会员分享,可在线阅读,更多相关《SQL Server 事务执行一半出错是否自动回滚整个事务(3页珍藏版)》请在金锄头文库上搜索。

1、大家都知道 SQL Server 事务是单个的工作单元。如果某一事务成功,则在该事务中进行的所有数据修改均会提交,成为数据库中的永久组成部分。如果事务遇到错误且必须取消或回滚,则所有数据修改均被清除。所以是不是说事务出错一定会回滚整个事物呢?先看几个个例子:-createtable create table testrollback(idintprimarykey, namevarchar(10)SETXACT_ABORTOFF -Default Settingsbegin traninsert into testrollbackvalues(1,kevin)insert into testr

2、ollbackvalues(2,kevin)insert into testrollbackvalues(1,kevin)insert into testrollbackvalues(3,kevin)commit tran三条成功插入只有第三条语句出错回滚-use SETXACT_ABORT ONSET XACT_ABORTON;begin traninsert into testrollbackvalues(1,kevin)insert into testrollbackvalues(2,kevin)insert into testrollbackvalues(1,kevin)insert

3、into testrollbackvalues(3,kevin)commit transelect *from testrollback全部回滚没有数据插入-use trycatch to catch error and rollback whole transcationbegin tranbegin tryinsert into testrollback values (1,kevin)insert into testrollback values (2,kevin)insert into testrollback values (1,kevin)insert into testrollb

4、ack values (3,kevin)commit tranend trybegin catchrollbackend catch全部回滚没有数据插入对于上面的测试可以看到默认情况下 SQL Server 只是 Rollback 出错的语句而不是整个事物。所以如果想Rollback 整个事物的话可以通过 SET XACT_ABORT 选项设置或者使用 Try Catch 之类的捕获错误进行Rollback.对于出现网络问题会跟上面的结果有点不一样。在执行事务的过程中,如果 Client 断开,那么 SQL Server会自动 Rollback 整个事物。在 SSMS 客户端执行第一条语句,去

5、掉 commitTranSET XACT_ABORTOFF-Default Settingsbegin traninsert into testrollbackvalues(1,kevin)insert into testrollbackvalues(2,kevin)insert into testrollbackvalues(1,kevin)insert into testrollbackvalues(3,kevin)之后断开连接,在服务器上用 DBCC OPENTRAN 查看 open 的事务:间隔一段时间再执行发现 DBCC OPENTRAN 已经没有了,而且查询表数据也没有,说明整个事

6、物回滚了。所以在客户端断开且事务没有完成的情况下整个事物回滚。对于上面的测试微软有详细的解释: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 clients network connection to an instance of theDatabase Engine is broken, any out

7、standing 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 connectio

8、ns 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 genera

9、ted 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)

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号