We have an application running on clinet which is used by about 30 concurrent users. In the application there is a Sub which deletes and inserts records in the underlying SQL-Server-Database.
The ID is a primary key in table A. Table B is filled in dosomething(ByVal ID as Integer) based on various data coming from Table A. Entries in Table B have a reference to table A (B.ID_A).
Due to the fact that this sub needs to be called from various places in the code and the number of concurrent users running the app at the same time we had some troubles with race conditions in the past. This resulted in data duplication because the code was running simultaneously.
Public Sub dosomething(ByVal ID As Integer)
End Sub
My question is if there is any way to prevent concurrent execution of this sub with the same ID at the same time in a multi-user application?
I've read about SyncLock but this doesn't solve the problem when running multiple instances of the application, right? Another solution i could think of is creating a Database Table where IDs are temporarily inserted as primary key at the beginning of the sub. This would result in the later execution getting an SQL-Exception when trying to insert a duplicate primary key i guess.