[转]sql server transaction
本文转自:
http://www.2cto.com/database/201208/146734.html
sql事务(Transaction)用法介绍及回滚实例
仔细研究了下,发现sql server里面的explicit transaction还是有点复杂的。以下是有些总结:
· Commit transaction 会提交所有嵌套的transaction修改。但是如果嵌套的transaction里面有rollback tran to save point, 那么save point之后的部分会revert掉。
delete from dbo.numbertable
begin tran out1
insert into dbo.numbertable values(1)
insert into dbo.numbertable values(2)
begin tran inn1
insert into dbo.numbertable values(3)
insert into dbo.numbertable values(4)
save tran inn1SavePoint
insert into dbo.numbertable values(5)
rollback tran inn1SavePoint
commit tran inn1
commit tran out1
· @@TRANCOUNT可以用来记录当前session transaction的个数,对于嵌套的transaction来讲,每次begin transaction都让它加一,每次commit tran都会让它减一。所以在语句里面可以通过select @@TRANCOUNT 来检查当前是否在一个transaction里面。如果当前@@TRANCOUNT为0,那调用commit还是rollback都会出现语句错误。在嵌套的transaction里面,rollback是很特殊的,它会直接把@@TRANCOUNT设置为0。
begin tran
begin tran
begin tran
print @@trancount
rollback tran
print @@trancount
· 对于嵌套的transaction来讲,rollback的写法是很特殊。如果嵌套,rollback transaction后面是不能带transaction的name的,要带也只能是最外面的transaction的name。Rollback只会抛弃所有嵌套transaction在rollback语句之前的修改。Rollback之后的更新依然提交就去了,原因在于:rollback之后,@@trancount为0,那么rollback之后的语句就不属于explicit transaction, 属于autocmmit transaction了,自动提交。
delete from dbo.numbertable
begin tran t1
insert into dbo.numbertable values(1)
begin tran t2
insert into dbo.numbertable values(2)
rollback tran
print 'after rollback in innert transaction, the transaction count is: '+cast(@@trancount, varchar(5))
insert into dbo.numbertable values(3)
--commit tran
select * from dbo.numbertable
· 存储过程里面也可以begin transaction,如果调用的地方也begin transaction,那么这种情况也属于嵌套transaction,如果在存储过程里面rollback,得到的结果和上面一样。但是有一点特殊的地方在与,执行存储过程结束的时候会比较开始执行sp的@@trancount和结束时候@@trancount的值,如果不一样,它会给出一个消息像“Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 0.”这个给出的消息并不会影响其后的执行。
CREATE PROCEDURE [dbo].[AddNumber]
AS
BEGIN
begin tran
insert into dbo.numbertable values(1)
insert into dbo.numbertable values(2)
insert into dbo.numbertable values(3)
rollback tran
END
delete from dbo.numbertable
begin tran out1
exec dbo.addnumber
print @@trancount
insert into dbo.numbertable values(3)
select * from dbo.numbertable
· 如果在sp里面rollback了,那到外满做commit, 或者rollback都是没有效果并且出错了,因为嵌套的transaction内部transaction一旦调用了rollback,@@trancount就为0了,在外满commit,rollback直接出错。比如如下sp,我想像在最外面rollback,那就出错了,因为sp里面语句rollback了。表里面始终会插入值3。
delete from dbo.numbertable
begin tran out1
exec dbo.addnumber
print @@trancount
insert into dbo.numbertable values(3)
rollback tran out1
select * from dbo.numbertable
· 所有对于嵌套的transaction来讲,如果内部transaction一旦rollback,就会给外部的transaction留下一个大坑。为了解决这个为题,有两种解决方案:
1. 在外部的transaction里面检查@@trancount,如果这个值跟你代码begin tran的可以一致,那说明内部transaction没有rollback,那可以继续commit或者rollback。
delete from dbo.numbertable
begin tran t1
insert into dbo.numbertable values(1)
begin transaction t2
insert into dbo.numbertable values(2)
rollback tran
if @@trancount = 1
begin
insert into dbo.numbertable values(3)
commit tran
end
2. 在所有的内部transaction里面,只能commit,不能rollback。如果必须rollback,那怎么办?save point就可以派上用场了。比如sp改成这样子:
ALTER PROCEDURE [dbo].[AddNumber]
AS
BEGIN
begin tran
save tran pp
insert into dbo.numbertable values(1)
insert into dbo.numbertable values(2)
insert into dbo.numbertable values(3)
rollback tran pp
commit tran
END
begin tran out1
exec dbo.addnumber
print @@trancount
insert into dbo.numbertable values(3)
commit tran out1
[转]sql server transaction的更多相关文章
- SQL Server Transaction Log Truncate && Shrink
目录 什么是事务日志 事务日志的组成 事务日志大小维护方法 Truncate Shrink 索引碎片 总结 什么是事务日志 Transaction log 是对数据库管理系统执行的一系列动作的记录 ...
- SQL SERVER TRANSACTION 事物
1.事务的概念 事物是一种机制,是一种操作序列,它包含了数据库一组操作命令,这组命令要么全部执行,要么都不执行.因此事物是一组不可分割的事物逻辑单元,在数据库进行并发操作时候,事物是作为最小的控制单元 ...
- SQL Server 数据库的维护(三)__事务(transaction)和锁
--维护数据库-- --事务(transaction)和锁-- --事务(transaction)-- --概述: 事务是指封装了一组T-SQL语句的单个逻辑单元.单元中的所有语句作为一个整体,在满足 ...
- SQL Server does not purge row versioning records even the transaction are committed if there are other open transaction running in the databases with read-committed snapshot enabled .
This is a by-design behavior. There is only one allocation unit in tempdb that istracking the versio ...
- SQL Server中事务transaction如果没写在try catch中,就算中间语句报错还是会提交
假如我们数据库中有两张表Person和Book Person表: CREATE TABLE [dbo].[Person]( ,) NOT NULL, ) NULL, ) NULL, [CreateTi ...
- [转]How to nest transactions nicely - "begin transaction" vs "save transaction" and SQL Server
本文转自:http://geekswithblogs.net/bbiales/archive/2012/03/15/how-to-nest-transactions-nicely---quotbegi ...
- sql server 学习笔记 (nested transaction 嵌套事务)
什么时候会用到嵌套事务 ? 为了代码复用,我们会写许多的储蓄过程,而中间如果需要使用到 transaction 难免就会发生嵌套了. sql server 并不直接支持嵌套事务. 但它可以用一些招式来 ...
- SQL Server 2008 中收缩数据库(DUMP,TRANSACTION,TRAN,无效,语法错误)
从SQL SERVER 2008 开始,我们已经不能再用以前 DUMP TRAN 数据库名 WITH NO_LOG 的这种方式来收缩数据库,但是,可以用另外一种替代的方法,SQL语句如下: ALTER ...
- 理解Sql Server 事务隔离层级(Transaction Isolation Level)
关于Sql Server 事务隔离级别,百度百科是这样描述的 隔离级别:一个事务必须与由其他事务进行的资源或数据更改相隔离的程度.隔离级别从允许的并发副作用(例如,脏读或虚拟读取)的角度进行描述. 隔 ...
随机推荐
- 线程同步工具 Semaphore类使用案例
参考博文 : 线程同步工具(一) 线程同步工具(二)控制并发访问多个资源 并发工具类(三)控制并发线程数的Semaphore 使用Semaphore模拟互斥锁 当一个线程想要访问某个共享资源,首先,它 ...
- Commons CLI 学习(1)
The Apache Commons CLI library provides an API for parsing command line options passed to programs. ...
- Highcharts创建一个简单的柱状图
新建一个html文件,将highcharts引入到你的页面后,通过两个步骤我们就可以创建一个简单的图表了. 1.创建div容器 在页面的 body部分创建一个div,并指定div 的 id,高度和宽度 ...
- 深入理解计算机(CSAPP)资源汇总
用于资源记录. 视频: 卡内基梅隆大学 Introduction to Computer Systems CMU 15-213 Fall 作业(labs): Lab Assignments
- SEO优化:WordPress发布文章主动推送到百度,加快收录保护原创
工作实在太忙,也没时间打理网站.最近公司额外交待了一些网站 SEO 方面的优化任务让我关注(这就是啥都要会.啥都要做的苦逼运维的真实写照了...). 于是抽空看了下百度站长平台,至少看到了2个新消息: ...
- Guid is not updated for cluster with specified cluster id; need to wait for hosts in this cluster to come up
http://mail-archives.apache.org/mod_mbox/cloudstack-users/201306.mbox/%3c201306181058330006472@gmail ...
- window 下 nginx+php+fastcgi 架设备忘
1.配置Php.ini 1)extension_dir = "./ext" 修改这个路径为真实的php的ext路径 2);extension=php_mysql.dll ;exte ...
- ASP.NET MVC 视图学习,纯干货
最近用MVC专门为自己做了一个网站,用来记录文章心情和日记.加上和同事的一些交流感觉颇深.所以想把13年买的MVC 4高级编程重新看一遍,记录一些东西,以后应该用的到.视图总是被控制器渲染,因为控制器 ...
- mysql查看表结构,字段等命令
mysql查看表结构命令,如下: desc 表名; show columns from 表名; describe 表名; show create table 表名;
- CodeForces 738A Interview with Oleg
模拟. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...