简体   繁体   中英

Spring Data Jpa - cache

Hello my problem is that i can't avoid cache. I'm using Spring Data Jpa with Spring Boot 1.5.4

What am I doing: I have a case when some client request my REST endpoint with some data based on which I am creating an entity and I'm saveing it into database, next I request another REST endpoint which response me dirlectly OK, but request which I got isn't finished yet. Next I'm waiting for another service which has to request my another REST endpoint (First client is all the time on wire). This endpoint modifies entity which was created after first request I got and here I got problem.

So basicly, first request creates entity and saves it using "saveAndFlush" method. When first request is waiting another thread modifies this entity using spring data jpa:

@Modifying(clearAutomatically = true)
@Query("UPDATE QUERY ")
@Transactional
int updateMethod();

But after that (when first request is released from waiting) when I call findOne method in first thread I got old entity, I have tried also override method:

@Transactional
default MyEntity findOneWithoutCache(Long id) {
    return findOne(id);
}

But this not working too, I also added

@Cacheable(false)
public class MyEntity {

And this is not working too.

There is only one way which is working, when i select this entity using @Query this way:

@Query("SELECT STATEMENT "
        + "WHERE p.id = ?1")
MyEntity findEntityById(Long id);

Could you explain me how to solve this problem?

The thing is what kind of Transaction Isolation do You have? What Database , settings, driver?

Theoretically in a perfect ACID transaction - after starting transaction you cannot see changes done in other transactions. (see Repeatable Read).

On the other hand typically you do not have I in ACID. And isolation is weaker. (like Read Commited).

If the query works it suggests you do not have Repeatable read - so maybe you should simply get EnityManager (via JpaContext) and try to clear() session (in the 1st thread)?

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