C
C#Lebanese Putin

concurrency and atomicity Asp.net core web api and efcore

let's say there's an endpoint called AssingUser() ,this endpoint assigns one donation to this current user who requested it, the idea is that , in the mobile application front end, the view of assignment is shared between many users , so let's say two users clicked at the same time to be assigned to this current donation, how can i make sure that one and only one is able to be set at this same time ? I'm not sure if i delivered what i want clearly 😅
P
Pobiega44d ago
There are a ton of solutions to this, naturally. The simplest way is to just query the object status before doing the actual assigning, thus checking that the status is still available
E
Esa44d ago
Some databases support this select * from X where Y for update. What that does is return a result that is locked on a row-level, meaning that returned result cannot be changed by other transactions. This should equate to an optimistic locking, which I think is sufficient for your usecase. The big benefit to this is that it happens in the same transaction as you fetch the entry, meaning you don't need two roundtrips to a) get the entry b) update its status, because it will be locked by that point.
LP
Lebanese Putin43d ago
yeah but can't the two requests be reading the same record at the same time ? or is the record automatically locked when some other transaction or query is reading it? looks nice , im using efcore with Sql server , any idea how to use this facility ?
P
Pobiega43d ago
Sure, and there are ways to solve that. Row locking on the database, or using something like Redis to acquire a shared lock, etc It all depends on what you are using and how your application is structured and scaled
LP
Lebanese Putin43d ago
let me clarify my concerns : let's say the logic is like that : - request comes - check if there's a user assigned to this donation - if not, assign this current user(update the record ) - if it's already assigned, return bad request ----------- my concerns are : both requests read that record and it isn't assigned yet, both will want to update this record
P
Pobiega43d ago
No I understand. The timing window for that is very small but its possible. And if that 100% must not be allowed, there are many ways to solve it. I suggested a few above. Another one is with an E-tag, a column on the database where each row has some randomly generated value when you fetch it, you read the e-tag. you then include the e-tag in your update statement
UPDATE TABLENAME SET X = y, etag = 'newuniquevalue' WHERE Id = 12312451 AND etag = 'uniquevaluefrombefore';
UPDATE TABLENAME SET X = y, etag = 'newuniquevalue' WHERE Id = 12312451 AND etag = 'uniquevaluefrombefore';
if the update fails, the etag has changed and you need to re-check if you still can access it
LP
Lebanese Putin43d ago
wait I'll try to understand this 😅
P
Pobiega43d ago
the only way to get the e-tag is to read it from the database. any edit to the row also updates the e-tag 🙂 this is how document databases deal with this scenario, usually
LP
Lebanese Putin43d ago
aaah ,like a unique stamp for like "who read this record"
P
Pobiega43d ago
sure
LP
Lebanese Putin34d ago
cool idea Alright thank you pobiega anyone interested on how i (not me efcore did) solved this problem ?
P
Pobiega34d ago
sure
LP
Lebanese Putin34d ago
okki so
LP
Lebanese Putin34d ago
i just added this 😭
No description
LP
Lebanese Putin34d ago
😭
LP
Lebanese Putin34d ago
P
Pobiega34d ago
Ah, its essentially an e-tag
LP
Lebanese Putin34d ago
yes
P
Pobiega34d ago
if combined with .ValueGeneratedOnAddOrUpdate() its even automated. nice.
LP
Lebanese Putin34d ago
but i was shocked how easy to implement it yeee in my case i don't want it to generat a value when reading or when writing so i just used IsConcurrencyToken
Want results from more Discord servers?
Add your server
More Posts
Building a generic repository for mongodbhello folks I was thinking build a generic repository structure for mongodb. but someday if i want Reading large xml file from archive by using XmlReader in Parallel modeHello 👋. I am looking for how can I read data from archive xml file in Parallel mode. I have archiResizing the Console window - Console ApplicationHi there, I'm trying to resize the console window in a Console Application. I have tried multiple ✅ Creating a datetime with a timezoneHow do I create a `DateTime` with `new DateTime(2010, 05, 12);` and give it a specific `TimeZoneInfoCustom Newtonsoft JSON deserializationI have a dictionary where: - The key will always be a string - The value will be either: - string Issue with Form.Close() in WinformsI have a custom form called CustomMessageBox In that form, I have a method that defines a button clIssue with IMGUI.Net.Hello, I am creating an ImGUI application and It doesn't let me move the ui across my entire screen,Issue with deserializing Enums in saved JSONHey, I've been trying to deserialize a list of cards I serialized in C#, the issue I'm having is it From HttpClient, can I save/copy the request to share with someone?Is there a way to copy raw request details? For example, I'm looking for a similar output like: -3D Unity RaycastingHi guys, currently working on a puzzle game involving lasers and mirrors, however, have hit a brick Computer Science - Computer ArchitectureOn a 64 bit system (where the bus length and word size are 64 bits) how would we determine the ammouConnecting SQLite in MAUIHow to connect the database without absolute path? If with it, the program works only on the computeDifferent HttpClient headersWhen I make an HTTP request in my browser (Chrome), I get different response headers than I do in myHttpRequestException:An error occurred while sending the request. WebExceptionHttpRequestException:An error occurred while sending the request. WebException:Unable to connect to ✅ EF does not put datetimes properly to the datebaseI am writing a backend app in asp.net core. This is my service code : ```c# public virtual async TaShadow Prop Error with IdentityDbContextThis is my IdentityDbContext: public class ApplicationUser : IdentityUser { [Required(ErrorMessWpf custom button template probleHi! I am trying to create custom button control, but I have a problem with a content property. ButtoWinfoms problem in Hosts fileI am writing a mini site blocker, the program has 2 functions to block the site and add the site thaDTO validation with DRY principle (without repeating the same validation)Hello, I would like to know what is used in practice, regarding the DTO validation. Let's say for 2 I'm using fedora, and i want create some .NET formsWhat should i do to create projects with ASP.NET forms in Linux system(Fedora)?