【发布时间】:2014-07-09 09:34:54
【问题描述】:
我正在尝试为此找到明确的答案。如果在可序列化事务中使用 SQL Server 上的 Linq 选择然后更新,是否存在死锁风险?例如:
using (var trans = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable, Timeout = new TimeSpan(0, 0, 10) }))
{
using (var con = new MyContext())
{
var record = con.MyTable.Single(t => t.id == 1); // share lock?
record.field = 99;
con.SubmitChanges(); // upgraded to exclusive lock? possible deadlock
trans.Complete();
}
}
而且由于这是一种常见的模式,因此避免它的唯一方法是:
var record = con.ExecuteQuery<MyTable>("select * from MyTable with (updlock) where id = {0}", 1).Single();
【问题讨论】:
标签: sql-server linq deadlock