Are there concurrency issues when adding updates to a statement

I would like to track the branches and how much money they hold. I want to have a Branches table with a Balance column. I will keep a log of transactions performed by the branch.

I will have applications that follow these instructions ...

 UPDATE Branches SET Balance = Balance + @Amount WHERE BranchID = @BranchID 

I will have a situation where UserA increases the balance from 100 to 120, and UserB increases the balance from 100 to 180 and thereby plays the change made by UserA

+4
source share
3 answers

[Applies to SQL Server ), but should be somewhat the same for other RDBMSs]
The first user who starts updating this row will exclusively block the row, so the second user cannot start the update at the same time.

By the time the second user transaction is completed, the value will already be updated to 120, so the second update will go from 120 to 200.

+1
source

A lock is activated in a table whenever an UPDATE or INSERT is executed in that table. Because of this lock, it is not possible to have such conflicts.

User 1 will update the value and after completion of the transaction, User 2 will also be able to update the value.

RDBMSs respect the principles of ACID , and thus distinguish between different operators. You should not worry about the conflicts of this issue.

0
source

This should not be a problem with any SQL server if the table is incorrect. What usually happens is that the SQL server locks the table for the update and then releases it for the next update. If requests arrive simultaneously, one will be executed before the other, but not one will be thrown out.

0
source

Source: https://habr.com/ru/post/1484074/


All Articles