c# - parallel.invoke transaction scope entity framework -


In my EF4 program, I have an applicant and application table. Many examples of the program run from time to time to make applications for applicants based on some business logic. In the application form, I can not have more than one submission / record for an applicant.

So here's the piece of code that checks whether there is a submitted application and it is included. It is run in a foreach loop for the list of applicants. Public application SaveApplication (using Int32 applicantId) {TransactionScope txScope = New TransactionScope (TransactionScopeOption.RequiresNew) {if (ApplicantHasPendingApplication (applicantId)) blank return; App app = zero; Try {app = new app () {// create item ...}; _unitOfWork.DisclosureApplications.Add (application); _unitOfWork.Commit (); _unitOfWork.Refresh (app); // We save it to get the ID and refresh txScope.Complete (); } Hold (pre-update before) {// We get an exception exception, where many instances attempt to insert the application} Return apps; }}

Above the code, prevents duplicate records from being inserted separately from the fact that it throws UpdateException while running several instances of the program. If I swallow that exception and move on then everything is fine.

However, I tried to test / run the code in parallel but it includes duplicate records in the database.

  Parallel.Invoke ((=) CreateApplications ("Parallel Example 1"), () = & gt; CreateApplications ("Parallel Example 2")); Private Zero CreateApplications (String Dummy Instance Name) {var unitOfWork = New SqlUnitOfWork (); Var Applicants = UnitOffork Applications.findell (). Take (100) .Olist (); Var Mask = New Provider Facade (Unitofwork, New Log 4 Netlogger (Dummy Instance-name)); Foreach (applicant applicant applicant) (Mask Application Provider. Approval (Applicant. APPLICANT ID);}}   

In the upper part of the code, this update throws exception and multiple applications for an applicant The row is inserted.

Note that the table contains only one primary key of rental and no other unique obstacles.

My question is: Why TransactionScope is in Parallel.Invoke Running duplicate rows included But does not I have many examples of program fire off? What a sound approach it will have to achieve

UPDATE: SqlUnitOfWork has ctor

  public SqlUnitOfWork () {_context = New MyEntities ();}   

MyEntities are generated by ctor ef -

  public constant string ConnectionString = "name = units"; Public Construction String ContainerName = "Unit"; Public TPIEntities (): Base (Connection String, Container Name) {this.ContextOptions.LazyLoad IngEnabled = true;}   

Thanks.

This is a classic race condition. Both transactions check to see if there exists a pre-existing application . Both of them find that there is no one, then try to include both of them.

You need to synchronize your check, either by locking in the application or by taking a SQL server UPDLOCK, HOLDLOCK by checking whether applications is present.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -