I’m creating a Java transfer money app that basically transfers money from one account to another.
In a nutshell I have a Transfer
entity that contains 3 properties: @ManyToOne OriginAccount
, @ManyToOne TargetAccount
and Amount
.
Account contains a balance to be adjust within the transfer.
I’m about to use LockModeType.PESSIMISTIC_WRITE
on Account
entities but I have to consider a deadlock.
One option is to select list of 2 accounts always in the same order (sort based on id) to always acquire locks in the same order.
I also heard about PessimisticLockScope.EXTENDED
but what will be the order of locks acquiring on joined records? Is there any option to ensure the order based on a kinda comparator? How can I certainly exclude a deadlock possibility?