none
SQL 2008 EF 6 WCF service causing deadlocks RRS feed

  • Question

  • I do three inserts in my WCF service. Two of them inside another library [class library project] and finally one inside the service itself. I use the same context for all 3 inserts so that I can roll back all 3 if the 3rd fails for example.

    My DBContext is set up based on unity and dependency injection:

    [Microsoft.Practices.Unity.Dependency, RegisterDependency(typeof(EmployeeEntities))]
    public EmployeeEntities EmployeeDb { get; set; }

    And here's how my code looks like for performing inserts.

    using (TransactionScope tr = new TransactionScope(TransactionScopeOption.Required,new TransactionOptions{IsolationLevel=System.Transactions.IsolationLevel.ReadUncommitted}))
                {
                using (DbContextTransaction dbTransaction = EmployoeeDb.Database.BeginTransaction()) 
    library.insertOne(EmployeeDb)
    library.insertTwo(EmployeeDb)
    EmployeeDb.Add(something); EmployeeDb.SaveChanges()

    Now everything works fine if I call my service sequentially. However when I simulate a load test, introduce some concurrent calls, the service starts to fail.

    Here's the error I see on Traces:An error occurred while updating the entities. See the inner exception for details: Transaction (ProcessId 128) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction

    I do some selects in my class library to compare against existing data. Those selects I am guessing are taking a lot of time. I don't have the transactionscope readuncommited on my libraries. I just pass the context, do my selects and inserts with it.

    Tuesday, April 28, 2015 8:30 PM

Answers

  • If you want to stop the deadlocks, then the link should help you, which you should apply to stopping deadlocks when reading too.

    http://www.c-sharpcorner.com/UploadFile/ff2f08/prevent-dead-lock-in-entity-framework/

    Thursday, April 30, 2015 3:40 PM
  • Hello PrivatePyle1982,

    You opened another transaction by using as “   using (DbContextTransaction dbTransaction = EmployoeeDb.Database.BeginTransaction()) “, please have a try to remove it and leave code to be:

    using (TransactionScope tr = new TransactionScope(TransactionScopeOption.Required,new TransactionOptions{IsolationLevel=System.Transactions.IsolationLevel.ReadUncommitted}))
    
    { 
    
    library.insertOne(EmployeeDb);
    
    library.insertTwo(EmployeeDb);
    
    EmployeeDb.Add(something); 
    
    EmployeeDb.SaveChanges();
    
    }

    If it does not work, please feel free to let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, April 30, 2015 6:21 AM
    Moderator

All replies

  • Hello PrivatePyle1982,

    You opened another transaction by using as “   using (DbContextTransaction dbTransaction = EmployoeeDb.Database.BeginTransaction()) “, please have a try to remove it and leave code to be:

    using (TransactionScope tr = new TransactionScope(TransactionScopeOption.Required,new TransactionOptions{IsolationLevel=System.Transactions.IsolationLevel.ReadUncommitted}))
    
    { 
    
    library.insertOne(EmployeeDb);
    
    library.insertTwo(EmployeeDb);
    
    EmployeeDb.Add(something); 
    
    EmployeeDb.SaveChanges();
    
    }

    If it does not work, please feel free to let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, April 30, 2015 6:21 AM
    Moderator
  • If you want to stop the deadlocks, then the link should help you, which you should apply to stopping deadlocks when reading too.

    http://www.c-sharpcorner.com/UploadFile/ff2f08/prevent-dead-lock-in-entity-framework/

    Thursday, April 30, 2015 3:40 PM