|
SQL SERVER事務處理(七)時間:2009-05-31 15:21:36來源:網(wǎng)絡 作者:未知 點擊: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:
在一系列嵌套的事務中用一個事務名給多個事務命名對該事務沒有什么影響。系統(tǒng)僅登記第一個(最外部的)事務名?;貪L到其它任何名字(有效的保存點名除外)都會產(chǎn)生錯誤。
事實上,任何在回滾之前執(zhí)行的語句都沒有在錯誤發(fā)生時回滾。這語句僅當外層的事務回滾時才會進行回滾。
例:內(nèi)部事務回滾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)部事務提交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: 設置TimeOut 參數(shù)
Set Lock_TimeOut 5000
被鎖超時5秒將自動解鎖
Set Lock_TimeOut 0
產(chǎn)立即解鎖,返回Error 默認為-1,無限等待
2:
(SET TRANSACTION ISOLATION LEVEL
{ READ COMMITTED
| READ UNCOMMITTED
| REPEATABLE READ | SERIALIZABLE})
READ COMMITTED
指定在讀取數(shù)據(jù)時控制共享鎖以避免臟讀,但數(shù)據(jù)可在事務結(jié)束前更改,從而產(chǎn)生不可重復讀取或幻像數(shù)據(jù)。該選項是SQL Server 的默認值。
避免臟讀,并在其他session 在事務中不能對已有數(shù)據(jù)進行修改。共享鎖。
READ UNCOMMITTED
執(zhí)行臟讀或 0 級隔離鎖定,這表示不發(fā)出共享鎖,也不接受排它鎖。當設置該選項時,可以對數(shù)據(jù)執(zhí)行未提交讀或臟讀;在事務結(jié)束前可以更改數(shù)據(jù)內(nèi)的數(shù)值,行也可以出現(xiàn)在數(shù)據(jù)集中或從數(shù)據(jù)集消失。該選項的作用與在事務內(nèi)所有語句中的所有表上設置 NOLOCK 相同。這是四個隔離級別中限制最小的級別。
REPEATABLE READ
鎖定查詢中使用的所有數(shù)據(jù)以防止其他用戶更新數(shù)據(jù),但是其他用戶可以將新的幻像行插入數(shù)據(jù)集,且幻像行包括在當前事務的后續(xù)讀取中。因為并發(fā)低于默認隔離級別,所以應只在必要時才使用該選項。
SERIALIZABLE
在數(shù)據(jù)集上放置一個范圍鎖,以防止其他用戶在事務完成之前更新數(shù)據(jù)集或?qū)⑿胁迦霐?shù)據(jù)集內(nèi)。這是四個隔離級別中限制最大的級別。因為并發(fā)級別較低,所以應只在必要時才使用該選項。該選項的作用與在事務內(nèi)所有 SELECT 語句中的所有表上設置 HOLDLOCK 相同。
本篇文章來源于:開發(fā)學院 http://edu. 原文鏈接:http://edu./2009/0531/5058.php
|
|
|