I have an application which issues letters to users in sequential order. This is the database structure

UserID  LastIssued
User1   2
User2   3
User3   0
User4   3

Admin users are able to issue letters (increment the number) to user. There are multiple admins who are able to login parallelly in the system.

This is the function which is used to issue letters

private void issueNewLetterToUser(String userid)
    int lastIssued;
    lastIssued = getLastIssuedForUserID(userid);

While the execution is at checkpoint 1 in the code, suppose another admin calls the same function for the same user, next letter will be issued by two admins. I want to prevent this. Also, if the function call is for some other user, the execution should be allowed because the database rows are independent.

How can I achieve this? (The question is specific to C# syntax, but still applicable to all languages)

1 Answers

Community On

This is a great case scenario for the transaction pattern.

Here is a good read about it: https://blog.matthewskelton.net/2005/08/22/better-transactions-with-delegates-in-csharp/

As a possible solution you might want to have the data update inside a transaction that can be accessed only by one admin at the time.