LazyInitializationException when trying to access individual objects left in Redis from RedisCacheManager

I am using Spring Redis data to cache sequential JPA objects in Redis using org.springframework.data.redis.cache.RedisCacheManager

Here is a way:

 @Override @Cacheable(value = MapCacheConfiguration.DATABASE_CACHE_NAME, key = "#root.method.name") public Curriculum findCurriculumByMemberId(Long memberId) { return curriculumRepository.findCurriculumByMemberId(memberId); } 

Unfortunately, after rebooting my boot application, the objects are still cached in Redis, and I get org.hibernate.LazyInitializationException

This may be due to the reason described in this message , i.e. access to an isolated object using hibernate - in my case, a serialized object left in the cache.

Can anyone advise a strategy to solve this problem?

  • Do I have to clear / delete the cache when destroying my application, given that the process of re-filling the cache is expensive and the application will be located in the cloud (Heroku), where dinosaurs / containers will be destroyed and recreated (and therefore restarted) quite often.
  • Or is there a way to connect to cached objects in entity manager?
  • Is there a better solution?
0
spring-cache spring-data-jpa spring-data-redis hibernate jpa
source share
1 answer

Redis is never used, but if your cache needs to be consistent across all application restarts, you might want to get all the necessary relationships before caching the object. Another problem may be that these cached objects are disconnected, which can be a problem if you want to use them again in transactions. Of course, you can connect them by calling merge(entity) , which, on the other hand, can cause problems with overriding new data with cached data.

Another persistent cache problem is class versions, because if you change classes between redeployments, deserialization will fail if the cache is already full with instances of the previous version of the class.

+1
source share

All Articles