-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalid record created when handling Concurrency using transaction in Microsoft Azure Sql #19792
Comments
@RaguMP This sounds like the kind of behavior that happens if However, I don't believe the behavior here should be different between SQL Server and SQL Azure. If you post a small, runnable project that reproduces the behavior you are seeing then we can take a deeper look, |
@ajcvickers: I have created a sample which reproduce the reported issue. I have created this sample using local database and attached the necessary SQL queries in the sample project too. Please find the uploaded sample in this github location and you can create the table in Azure SQL to check the issue by using the attached DB queries. Note: Initially when I reported this issue I could not reproduce this issue in SQL server and so stated like the issue occurred in Azure SQL, but in this sample it is producing even in local database too. I am not able to find in which scenario this is not reproduced in SQL server previously. Kindly check the sample and let me know if any additional detail needed from my end. |
@RaguMP As I wrote above, the transaction needs to be rolled back then the |
@ajcvickers: I have tried to rollback the transaction once Note:
Kindly check the sample and let me know if I had done anything wrong. |
@RaguMP I spent some time debugging into your code to understand what you are trying to do. Consider this code: var sequenceIdDetails = await context.TrProjectSequenceMapper.FirstOrDefaultAsync(i => i.IsActive && i.BrandId == brandId)
.ConfigureAwait(false);
projectDetails.BrandProjectId = sequenceIdDetails.SequenceId;
sequenceIdDetails.SequenceId++; The fist time through this code loads the The second time through (i.e. in the retry) the context is always tracking the Ways to fix this:
|
@ajcvickers : Thanks for checking into this problem and for your workaround suggestion. But still I am facing the same issue. I have tried to detach the Note: If I don't refresh original values once after concurrency is occurred I am not getting updated with the latest value (i.e. In this sample
Kindly let me know whether I am in a proper way as per your suggestion or done any wrongly. If so kindly share me sample code snippet to overcome this problem. |
I have handled Optimistic concurrency control in my case and used transactions. I am facing an issue where when an concurrency is occurred and while re-updating the record, creating an invalid row with the old value which I have read before concurrency occurred.
For example: I have three tables as follows.
ProjectCategorySequenceMapper - To maintain Sequence value based on project category
ProjectCategory - To maintain Project Category
Projects - To maintain Project details based on project category
I have used "RowVersion" column in "ProjectCategorySequenceMapper" table for concurrency check.
My goal is to read the sequence value based on project category from "ProjectCategorySequenceMapper" table when I am creating a new project and need to increment the sequence value by one and update in "ProjectCategorySequenceMapper" table.
In this scenario, when I try to create a new project (i.e. expected sequence Id - 3) under category (i.e. CategoryId - 1), once I read the value from database and concurrency occurred and in database sequence Id is updated as 4. I have handled to re-update concurrency and an invalid entry with Project Id 3 is created based on sequence value which is read before concurrency and new expected record created with Project Id 4 when I am using transaction. Without transaction everything works fine as expected whereas an invalid record created when using transaction.
Note:
In Microsoft Azure Sql Database only this invalid record created when using transaction whereas in normal Sql Database this issue not reproduced.
Kindly let me know is this an issue or anything done wrongly in my side.
Further technical details
EF Core version: 3.1.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
IDE: Visual Studio 2019 16.4.4
The text was updated successfully, but these errors were encountered: