简体   繁体   中英

Open transaction cannot see committed data by another transaction

I have a spring boot app, a REST service is called inside this rest call two separate DB operations are done (Spring Data JPA is being used). First one updates some database tables and second one reads from the said tables and do some operations.

@GetMapping("/process")
public void process(){
   service1.updateValues();//updates the values
   service2.readValuesAndProcess();//reads those updated values and execute some logic and persist at the end
}

Now the problem is;

Second method doesn't seem to read the updated values that are updated in the first method and somehow reads the values before the update. Both these methods are @Transactional , Entities are @versioned and when a save operation is called ObjectOptimisticLockingFailureException is thrown.

In application.properties spring.jpa.open-in-view property is true, when i change it to false everything works as expected but i can't guarantee that i won't get a lazyInitializationException somewhere else.

I tried changing transactional methods propagation to REQUIRES_NEW and nothing changes.

How are you doing the update in updateValues ? is the code thread safe for concurrent users? Lost updates might cause JPA to throw ObjectOptimisticLockingFailureException which is a good thing.

Please read this for more information about Optimistic Locking: Optimistic Locking in JPA | Baeldung .

If updateValues() is annotated as @Transactional, it should work. At the end of updatevalues() the changes will be actually committed to database by spring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM