We are using Optimistic Locking with JPA and it seems to work perfectly within each multi-threaded service no matter how many threads we add, but when two services running as separate apps are updating the same columns in a table they overwrite each other (dirty reads). Is Hibernate optimistic locking not guaranteed to work across separate JVMs? I've had a hard time finding any info on this.
We are using JPA with hibernate under the hood. Optimistic locking is working, at least between the threads inside each service, I can see it tripping exceptions and trying in the retry listener i added.
Here is some code
@Transactional
@Retryable(value = {Exception.class}, maxAttemptsExpression = "${locking.max.retry.count:30}",
backoff = @Backoff(delayExpression = "${locking.retry.sleep.time:10}"),
listeners = "accountUpdateRetryListener")
public void balancesAndAmtsUpdate(TransactionSet transactionSet) {
// new entity versions are picked up in here
mapTransactionsToAccounts(transactionSet);
groupAccounts(transactionSet);
// update Accounts
transactionSet.getAccountPostingAggregators().forEach( agg -> accountUpdate(agg, transactionSet));
transactionSet.getNettingAggregators().forEach( agg -> {
if (transactionSet.getDigestForPosting()) {
accountNetTotalsUpdate(agg);
}
accountsNetSourceUpdate(transactionSet, agg);
});
}