1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
1 如何锁一个表的某一行
SET
TRANSACTION
ISOLATION
LEVEL
READ
UNCOMMITTED
SELECT
*
FROM
table
ROWLOCK
WHERE
id = 1
2 锁定数据库的一个表
SELECT
*
FROM
table
WITH
(HOLDLOCK)
加锁语句:
sybase:
update
表
set
col1=col1
where
1=0 ;
MSSQL:
select
col1
from
表 (tablockx)
where
1=0 ;
oracle:
LOCK
TABLE
表
IN
EXCLUSIVE MODE ;
|
1
2
3
4
5
6
7
8
9
10
11
12
|
在第一个连接中执行以下语句
begin
tran
update
table1
set
A=’aa’
where
B=’b2′
waitfor delay ’00:00:30′ –等待30秒
commit
tran
在第二个连接中执行以下语句
begin
tran
select
*
from
table1
where
B=’b2′
commit
tran
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
在第一个连接中执行以下语句
begin
tran
select
*
from
table1 holdlock -holdlock人为加锁
where
B=’b2′
waitfor delay ’00:00:30′ –等待30秒
commit
tran
在第二个连接中执行以下语句
begin
tran
select
A,C
from
table1
where
B=’b2′
update
table1
set
A=’aa’
where
B=’b2′
commit
tran
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
增设table2(D,E)
D E
d1 e1
d2 e2
在第一个连接中执行以下语句
begin
tran
update
table1
set
A=’aa’
where
B=’b2′
waitfor delay ’00:00:30′
update
table2
set
D=’d5′
where
E=’e1′
commit
tran
在第二个连接中执行以下语句
begin
tran
update
table2
set
D=’d5′
where
E=’e1′
waitfor delay ’00:00:10′
update
table1
set
A=’aa’
where
B=’b2′
commit
tran
|