How to Evict Entity in Cache When State Object Exception Is Encountered
I. Introduction StateObjectStateException normally happens when two concurrent threads/transactions are loading and modifying an entity with...
https://www.czetsuyatech.com/2020/05/java-evict-entity-in-cache-during-exception.html
I. Introduction
StateObjectStateException normally happens when two concurrent threads/transactions are loading and modifying an entity with the same version.
Let's say we have an entityA currently on version1, both threadA and threadB access this entity and perform an update. But threadA commits the transaction first, so the version is now 2. Note that threadB is still in version 1, thus when its transaction commits it will fail due to optimistic locking collision as it's expecting a version update from 1 to 2.
In this particular case, we are expecting that entityA is loaded in cache and when a StaleObjectStateException is encountered we evict it.
II. Solution
Here's what to do:
1.) Update your persistence.xml file and add:
<property name="hibernate.persister.resolver" value="org.hibernate.util.CustomPersisterClassResolver"></property>
2.) Extends StandardPersisterClassResolver. See the code below.
3.) Implement the SingleAutoStaleObjectEvictingPersister class. See the code below.
Post a Comment