鎖的認識lock

2021-07-22 13:00:59 字數 1288 閱讀 8229

case 1:事務正在更新一張table,並且該事務並沒有處理結束。此時,使用select查詢出來的結果是?

update之前的?之後的?始終沒有output,知道事務處理結束?

1):始終處於被堵塞狀態

begin tran

update student set name='change' where id=2 

--commit 

select * from  student where id=2 

2):begin tran

update student set name='change' where id=3

--commit

select * from  student with (nolock) where id=3 ;

不堵塞儲存過程,並且獲取資料時當前最新資料。

--在讀取或修改資料時不加任何鎖

select * from lock with (nolock) 

--其他事務可以讀取表,但不能更新刪除。查詢結束就釋放該鎖,不會阻塞其他儲存過程。 

select * from lock with 

(holdlock)

--其他事務可以讀取表,但不能更新刪除。事務結束才釋放該鎖,會!阻塞其他儲存過程。

select * from lock with 

(updlock)

case2:

--其他事務不能讀取表,更新和刪除 

select * from lock with 

(tablockx)

sql server可鎖定的資源:

共享鎖,使用者所有的唯讀資料操作,s。

修改鎖,鎖定被修改的資源。

獨佔鎖,?為修改資料而保留的x

架構鎖,?

意向鎖,?有獲得共享鎖和獨佔鎖的意向。ix

批量修改鎖

select * from sys.dm_tran_locks

sql server profile顯示所有事件,所有列。

set transaction isolation level repeatable read

可讀?可更新?

tablelock

髒資料堵塞

holdlock

堵塞堵塞

updlock

堵塞堵塞

lock執行緒鎖

lock 實現提供了比使用 synchronized 方法和語句可獲得的更廣泛的鎖定操作。lock鎖可以顯示的獲取鎖物件和釋放鎖,而synchorized則是隱式的。不使用塊結構鎖就失去了使用 synchronized 方法和語句時會出現的鎖自動釋放功能。lock 介面的實現允許鎖在不同的作用範圍內...

執行緒鎖Lock

from threading import thread,lock import time deffunc global n n 1n 10t list for i in range 10 t thread target func,t.start for t in t list t.join pri...

Lock鎖與synchronized鎖的區別

1 synchronized鎖是可以幫助我們自動開鎖和關閉鎖 2 lock鎖,我們最常用的是reentrantlock重入鎖,需要我們手動的開鎖和手動關鎖 3 synchronized只能與wait notify 方法一起使用 4 reentrantlock只能與condition類中的await ...