(Using Spring Boot 2.3.3 w/ MySQL 8.0.)
Let's say I have an Account
entity that contains a total
field, and one of those account entities represents some kind of master account. I.e. that master account has its total
field updated by almost every transaction, and it's important that any updates to that total
field are done on the most recent value.
Which is the better choice within such a transaction:
Using a
PESSIMISTIC_WRITE
lock, fetch the master account, increment the total field, and commit the transaction. Or,Have a dedicated query that essentially does something like,
UPDATE Account SET total = total + x
as part of the transaction? I'm assuming I'd still need the same pessimistic lock in this case for the UPDATE query, e.g. via@Query
and@Lock
.
Also, is it an anti-pattern to retry a failed transaction a set number of times due to a lock-acquisition timeout (or other lock-based exception)? Or is it better to let it fail, report it to the client, and let the client try to call the transaction/service again?
Apologies for the basic question, but, it's been some time since I've had to worry about doing such a thing in Spring.
Thanks in advance!