Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql table cell modified by multiple threads at the same time

if you have table BankAccount that has a column Amount and the value for this column for a specific row can be modified by multiple threads at the same time so it could happen so that the last one to set the value will win.

how do you usually handle this kind of situations ?

UPDATE: I heard that in MSSQL there is update lock UPDLOCK that locks the table or the row that is being updated, could I this here somehow ?

like image 853
Omu Avatar asked Feb 25 '26 21:02

Omu


1 Answers

An update statement which references the current value would prevent overwriting. So, instead of doing something like

SELECT Amount FROM BankAccount WHERE account_id = 1

(it comes back as 350 and you want to subtract 50)...

UPDATE BankAccount SET Amount = 300 WHERE account_id = 1

do

UPDATE BankAccount SET Amount = Amount - 50 WHERE account_id = 1
like image 112
Conspicuous Compiler Avatar answered Feb 28 '26 09:02

Conspicuous Compiler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!