【发布时间】:2016-05-08 00:45:22
【问题描述】:
在上下文中调用 SaveChanges() 时,所有插入/删除/更新操作都在单个事务中执行。也可以将 DbContextTransaction 用于事务。我正在尝试使用这两种方法来模拟死锁。当我使用 DbContextTransaction 时,我会立即得到死锁异常,但即使在一小时后,单独的 SaveChanges() 也不会引发任何死锁异常。我做错了吗?
这是带有 DbContextTransaction 的代码。我尝试更新主线程中的第一行,然后是第二行。我还开始了另一个任务,它尝试先更新第二行,然后再更新第一行。
while (true)
{
using (var context = new SchoolDBEntities())
{
using (System.Data.Entity.DbContextTransaction dbTran = context.Database.BeginTransaction())
{
Random r = new Random();
int r1 = r.Next();
int r2 = r.Next();
Student std1 = context.Students.First();
std1.StudentName = "test"+r1;
context.SaveChanges();
Student std2 = context.Students.Find(2);
std2.StudentName = "test"+r2;
context.SaveChanges();
dbTran.Commit();
}
}
}
但是当我尝试使用 SaveChanges() 时,它不会产生死锁:
while (true)
{
using (var context = new SchoolDBEntities())
{
try
{
Random r = new Random();
int r1 = r.Next();
int r2 = r.Next();
Student std1 = context.Students.First();
std1.StudentName = "test" + r1;
Student std2 = context.Students.Find(2);
std2.StudentName = "test" + r2;
context.SaveChanges();
}
}
}
我正在使用 SQL Profiler 来跟踪事务。我什至为第二种方法添加了更多更新,只是为了使该事务的持续时间等于 DbContextTransaction 案例,我认为这可能是原因,但仍然没有运气!当我查看跟踪时,我看到属于特定事务的更新仅在前一个事务提交后才开始。可能是什么原因?
【问题讨论】:
标签: sql sql-server entity-framework transactions deadlock