c# - Detecting concurrency issues in Entity Framework using transactions -


I've saved the code that is used to help reduce compatibility issues. Is:

  // HasConcurrencyIssues opens a new EntityContext and returns the actual current // details and then compares it with that which was loaded for the form (HesConcurrencyIssues ()) {// can not save, quit avoiding code returns; } (Using EntityContext reference = New EntityContext ()) to avoid reading // dirty dirty - should I use it? Isolation Level Level = Differential Level Read; (Using DbContextTransaction trans == context.Database.BeginTransaction (level)) (try {// here where I save items using references by adding items to dbSATs.SaveSthaf (reference); // Generate encrypted command Execute before check because // it can refer to some time.Service Changes (); // Check Changes () // After the execution, something happened, check again ** ** blocked here * * If (this is HesConcurrencyIssues ()) {// Do not save Exit rollback and code trans. Rollback (); Return;} trans.Commit ();} Hold (Exception pre) {// Handle error trans.Rollback ();}}}  < / pre> 

I was using this structure to help reduce the issues of concurrency, the problem I am facing is that I really do not understand how ReadCommitted and concurrency work together. I have noticed that my second position for concurrency issues This is blocked on the test because it is not able to read the current records (I'm assuming what to do with my chosen isolation level ). I am worried that if I remove that secondary examination then it can give birth to phantom data which I do not want.

The application is being expanded and 200 users should potentially be able to handle and edit the same object and save it. I basically want a hard stop which checks that the object changes And if so, stop saving one.

How can I make my defense code to reduce the issues of concurrency?

Edit

I know that it is being locked because the check is after the SaveChanges call if I < This will work if code is done before the code> save change call, but it is unlikely that the transaction can end, while saving is due which should be the reason for showing the concurrent issue

  • / Ul>

    It looks like this would be a simple approach: using

      (var ts = new transaction ()) {if (this.HasConcurrencyIssues ()) {// can not be saved Exit code return return; } (EntityContext context = new EntityContext ()) using {this.SaveStuff (context); Context.SaveChanges (); } Ts.Complete (); }   

    Because when you read the information from the database, the area of ​​your transaction has already started, unless you save that permission to change that information. Can not be given.

    Similarly, if any other thread has already read the information to determine whether there are any concurrency problems, then your call will not return to HasConcurrencyIssues () unless they change their changes Do not save.

    The transaction scope will automatically roll back to the end of the block by using if it is not completed, there is no need for additional effort / catch logic.

  • Comments

    Popular posts from this blog

    php - PDO bindParam() fatal error -

    logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

    java - Why my included JSP file won't get processed correctly? -