Looking for a best-practice advice:
Let's suppose I have a Account object with limit attribute. Each day there can be n Payments, with sum of their amounts up to the account limit. When creating a new payment, it checks to see if it's amount + amounts of other payments of the day are still within the account limit, and either saves the record or displays error.
Now, let's assume I have account with limit 100$, and simultaneously two payments of 99$ are being created. Each would do a select, see that nothing is there, and proceed to save itself, resulting in 198$ total being saved.
What would you do about this? I was thinking about issuing a write lock on the payments table at the start of transaction, but that seems quite heavy-handed, seeing that I only really care about not allowing payments belonging to specific account not being read by other transactions. Are there any other options, better ways of handling this situation?