SQL SERVER事務(wù)處理(七)時間:2009-05-31 15:21:36來源:網(wǎng)絡(luò) 作者:未知 點(diǎn)擊:203次 begin tran t1
----In the first trans . Insert into demo2(name,age) values('lis',1) ---Second Trans begin transaction t2 insert into demo values('BB','B term') commit transaction t2 ----In the first begin tran t1 ----In the first trans .
Insert into demo2(name,age) values('lis',1)
---Second Trans begin transaction t2
insert into demo values('BB','B term')
commit transaction t2
----In the first trans .
Insert into demo2(name,age) values('lis',2)
rollback transaction t1
Note:
在一系列嵌套的事務(wù)中用一個事務(wù)名給多個事務(wù)命名對該事務(wù)沒有什么影響。系統(tǒng)僅登記第一個(最外部的)事務(wù)名。回滾到其它任何名字(有效的保存點(diǎn)名除外)都會產(chǎn)生錯誤。
事實(shí)上,任何在回滾之前執(zhí)行的語句都沒有在錯誤發(fā)生時回滾。這語句僅當(dāng)外層的事務(wù)回滾時才會進(jìn)行回滾。
例:內(nèi)部事務(wù)回滾SQL server 報錯。
begin tran t1
Insert into demo2(name,age) values('lis',1)
---Second Trans
--Server: Msg 6401, Level 16, State 1, Line 6
---Cannot roll back t2. No transaction or savepoint of that name was found.
begin transaction t2
insert into demo values('BB','B term')
rollback transaction t2
----In the first trans .
Insert into demo2(name,age) values('lis',2)
commit transaction t1
例: 內(nèi)部事務(wù)提交SQL server 不會報錯。
begin tran t1
Insert into demo2(name,age) values('lis',1)
---Second Trans no error
begin transaction t2
insert into demo values('BB','B term')
commit transaction t2
----In the first trans .
Insert into demo2(name,age) values('lis',2)
commit transaction t1
SQL Server 的隔離級別:
1: 設(shè)置TimeOut 參數(shù)
Set Lock_TimeOut 5000
被鎖超時5秒將自動解鎖
Set Lock_TimeOut 0
產(chǎn)立即解鎖,返回Error 默認(rèn)為-1,無限等待
2:
(SET TRANSACTION ISOLATION LEVEL
{ READ COMMITTED
| READ UNCOMMITTED
| REPEATABLE READ | SERIALIZABLE})
READ COMMITTED
指定在讀取數(shù)據(jù)時控制共享鎖以避免臟讀,但數(shù)據(jù)可在事務(wù)結(jié)束前更改,從而產(chǎn)生不可重復(fù)讀取或幻像數(shù)據(jù)。該選項(xiàng)是SQL Server 的默認(rèn)值。
避免臟讀,并在其他session 在事務(wù)中不能對已有數(shù)據(jù)進(jìn)行修改。共享鎖。
READ UNCOMMITTED
執(zhí)行臟讀或 0 級隔離鎖定,這表示不發(fā)出共享鎖,也不接受排它鎖。當(dāng)設(shè)置該選項(xiàng)時,可以對數(shù)據(jù)執(zhí)行未提交讀或臟讀;在事務(wù)結(jié)束前可以更改數(shù)據(jù)內(nèi)的數(shù)值,行也可以出現(xiàn)在數(shù)據(jù)集中或從數(shù)據(jù)集消失。該選項(xiàng)的作用與在事務(wù)內(nèi)所有語句中的所有表上設(shè)置 NOLOCK 相同。這是四個隔離級別中限制最小的級別。
REPEATABLE READ
鎖定查詢中使用的所有數(shù)據(jù)以防止其他用戶更新數(shù)據(jù),但是其他用戶可以將新的幻像行插入數(shù)據(jù)集,且幻像行包括在當(dāng)前事務(wù)的后續(xù)讀取中。因?yàn)椴l(fā)低于默認(rèn)隔離級別,所以應(yīng)只在必要時才使用該選項(xiàng)。
SERIALIZABLE
在數(shù)據(jù)集上放置一個范圍鎖,以防止其他用戶在事務(wù)完成之前更新數(shù)據(jù)集或?qū)⑿胁迦霐?shù)據(jù)集內(nèi)。這是四個隔離級別中限制最大的級別。因?yàn)椴l(fā)級別較低,所以應(yīng)只在必要時才使用該選項(xiàng)。該選項(xiàng)的作用與在事務(wù)內(nèi)所有 SELECT 語句中的所有表上設(shè)置 HOLDLOCK 相同。
本篇文章來源于:開發(fā)學(xué)院 http://edu. 原文鏈接:http://edu./2009/0531/5058.php
|
|