Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

c# - Chosen as the deadlock victim on simple Add() -> SaveChanges()

This simple program

private static void Main(string[] args)
{
    Parallel.For(0, 1000000, InsertTestEntity);
}

private static void InsertTestEntity(int i)
{
    using (var dbContext = new TestDbContext())
    {
        dbContext.TestEntities.Add(new TestEntity { HugeString = "Baby shark," + string.Join(", ", Enumerable.Repeat("doo", 500)) });
        dbContext.SaveChanges();
    }
}

public class TestEntity
{
    [Key]
    public int Id { get; set; }

    public string HugeString { get; set; }
}

public class TestDbContext : DbContext
{
    public DbSet<TestEntity> TestEntities { get; set; }
}

Throws an exception like

System.Data.Entity.Infrastructure.DbUpdateException
  HResult=0x80131501
  Message=An error occurred while updating the entries. See the inner exception for details.
  Source=EntityFramework
  StackTrace:
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
   at EntityFrameworkStressTest.Program.InsertTestEntity(Int32 i) in c:GitEntityFrameworkStressTestEntityFrameworkStressTestProgram.cs:line 18
   at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.<ForWorker>b__1()

Inner Exception 1:
UpdateException: An error occurred while updating the entries. See the inner exception for details.

Inner Exception 2:
SqlException: Transaction (Process ID 100) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

The SQL table looks like this in SSMS

TestEntities table from SSMS

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As seen in the SSMS screenshot, there seem to be no indication of a primary key. There should have been a key symbol next to the ID column.

Some ID actually declared as primary key

Further inspection reveals that Id is indeed declared as identity (SQL Server for AUTO INCREMENT), but not as primary key. To make Id a real primary key right click the table in SSMS and choose Design, right click the Id row in the column designer and click SET PRIMARY KEY:

enter image description here

After this, the stress tests runs without deadlocking itself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...