Home Spring test updates locally read JPA entity during transaction
Reply: 0

Spring test updates locally read JPA entity during transaction

user1445 Published in July 21, 2018, 4:57 pm

This question already has an answer here:

  • How do you test Spring @Transactional without just hitting hibernate level 1 cache or doing manual session flush? 2 answers

I have a Spring integrated test that is wrapped in @Transactional. The Hibernate/JPA database interface extends JpaRepository and uses PESSIMISTIC_READ and PESSIMISTIC_WRITE locks on its functions. Within the test, the following steps occur:

  1. An @Entity object is read from the repo.
  2. The target function runs, which updates and writes that same @Entity to the repo inside another @Service-level transaction.
  3. That @Entity object is again read from the repo and compared to the first object.

The problem is that after writing in step 2, the first @Entity object has actually been updated locally. When comparing the @Entity objects in step 3, both are equal instead of having expected "before and after" differences.

How is this "syncing" of local @Entity objects happening, and is this expected behavior?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306304 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO