I implemented a service for an entity object and uses pure jpa , I used spring , so hibernate configured as jpa impl in spring xml config . I use spring data for crud operations. But in our system, our entity object is pulled / updated many times, and for data there is high competition ( concurrency ). From our code in many places there are many classes just inject bean service and calling the getEntity method to get the entity, after it they change the entity (which separates as my understanding, but in the same thread, em should be the same as in my knowledge), therefore it takes some time, when the object returns to the service being updated, it calls the save() method of the service to save the object. save() method is just a call to merge() crud operation. It is transactional with @Transactional annotation. Here the problem arises when someone pulls an entity object, and by changing it, someone else can pull and change it and save it back, so my entity is read, and if I save it, I will override the already updated object. The problem is that we change the entity outside the service and call it back. Here, the spring datastore classes are the DAO layer.
Optimistic lock is one solution, but for some reason we did not like it, so it does not work for us. I am thinking of a pessimistic lock . For example, when I get an entity to update by blocking, then I change it somewhere else and call back (the entity already blocked from updating!) Does it work? I'm not sure if there is an EntityManager object that I used to extend the object. If so, it takes quite a while to pass these smart logics before they are updated and unlocked.
Here is a simple sample code for the script:
class SomeEntity { Long id; String field1; String field2; String field3; String field4; String field5; String field6; String field7;
Here I canβt move this tons of logic inside the service layer, itβs quite specific, and this kind of logic is in 100 places.
So, is there a way to come and at least apply a pessimistic block to this situation?
source share