[转]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 事务隔离级别,百度百科是这样描述的 隔离级别:一个事务必须与由其他事务进行的资源或数据更改相隔离的程度.隔离级别从允许的并发副作用(例如,脏读或虚拟读取)的角度进行描述. 隔 ...
随机推荐
- [PAT] 1141 PAT Ranking of Institutions(25 分)
After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...
- 下载安装go插件包报错fatal: unable to access 'https://github.com/golang/tools.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054
使用git命令来给vscode安装go插件的时候报错,如下: $ git clone https://github.com/golang/tools.git tools Cloning into 't ...
- 小程序css
样式导入 @import /** common.wxss **/ .small-p { padding:5px; } /** app.wxss **/ @import "common.wxs ...
- LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ...
- <一>dubbo框架学前原理介绍
alibaba有好几个分布式框架,主要有:进行远程调用(类似于RMI的这种远程调用)的(dubbo.hsf),jms消息服务(napoli.notify),KV数据库(tair)等.这个框架/工具/产 ...
- Multiply Strings——面试题
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 讲IOC非常好的一篇文章--初步弄懂DI
http://jinnianshilongnian.iteye.com/blog/1413846 http://jinnianshilongnian.iteye.com/blog/pdf 之后又看了类 ...
- Redis实现分布式锁 php
一.分布式锁的作用: redis写入时不带锁定功能,为防止多个进程同时进行一个操作,出现意想不到的结果,so...对缓存进行插入更新操作时自定义加锁功能. 二.Redis的NX后缀命令 Redis有一 ...
- git reset用法
git 删除 错误 提交的 commit 方法: 根据–soft –mixed –hard,会对working tree和index和HEAD进行重置: git reset -- ...
- python打印所有汉字
n=0 for ch in xrange(0x4e00, 0x9fa6): print unichr(ch), n = n+1 if(n%50==0): print '\n' print n