I am storing financial transactions in a database. For efficiency reasons in the transaction row i save the ammount and also the balance of the account. For example:
ID | TRANSACTION_AMOUNT | ACCOUNT_BALANCE
ACCOUNT_BALANCE in a new row is calculated as the last ACCOUNT_BALANCE + the new TRANSACTION_AMOUNT.
To proceed with this operation i need to do first a SELECT and then an INSERT. The issue is if two transactions came almost at the same time. Both of them will read the same last ACCOUNT_BALANCE and after the inserts the ACCOUNT_BALANCE with be inconsistent.
I have seen the function lockForUpdate() but im not completly sure how to use it for this case or even if apply.
Whats the best approach to solve this?
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2m1YGNw
via IFTTT
Aucun commentaire:
Enregistrer un commentaire