【发布时间】:2016-06-13 02:54:13
【问题描述】:
我相信 SQL Server 中的每个SELECT 语句都会导致放置共享锁或键锁。但是它在事务中会放置相同类型的锁吗? Shared 或 Key 锁是否允许其他进程读取相同的记录?
例如我有以下逻辑
Begin Trans
-- select data that is needed for the next 2 statements
SELECT * FROM table1 where id = 1000; -- Assuming this returns 10, 20, 30
insert data that was read from the first query
INSERT INTO table2 (a,b,c) VALUES(10, 20, 30);
-- update table 3 with data found in the first query
UPDATE table3
SET d = 10,
e = 20,
f = 30;
COMMIT;
此时,我的 select 语句是否仍会创建共享锁或键锁,还是会升级为排他锁?其他事务是否能够从 table1 中读取记录,还是所有事务都等到我的事务提交后,其他事务才能从中进行选择?
在应用程序中,是否将 select 语句移到事务之外并仅将插入/更新保留在一个事务中?
【问题讨论】:
标签: sql sql-server transactions database-deadlocks locks