当SQL Server 2008上的多个进程同时运行以下事务时,会检测到死锁异常。
-- pseudo code
BEGIN TRANSACTION
-- Below line is done by JPA native query
SELECT * FROM T WITH (UPDLOCK, ROWLOCK) WHERE COL1 = ? and COL2 = ?
-- Below 2 lines are generated by JPA provider
SELECT COL1, COL2, COL3, COL4, COL5 FROM T WHERE COL1 = ? and COL2 = ?
...
UPDATE T SET COL3 = ? WHERE COL 1 = ? and COL2 = ?
END TRANSACTION
根据此参考http://support.microsoft.com/kb/179362,COL1和COL2被聚集索引并设置为主键。
我的问题是为什么UPDLOCK会发生死锁?第二次交易后来应该等待第一笔交易完成?