尝试访问由RedisCacheManager留在Redis中的分离对象时发生LazyInitializationException [英] LazyInitializationException when trying to access detached objects left around in Redis by RedisCacheManager

查看:219
本文介绍了尝试访问由RedisCacheManager留在Redis中的分离对象时发生LazyInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring数据Redis来缓存Redis中的序列化JPA实体,使用 org.springframework.data.redis.cache.RedisCacheManager

I use Spring data Redis in order to cache serialized JPA entities in Redis using org.springframework.data.redis.cache.RedisCacheManager

以下是方法:

Here is the method:

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

不幸的是,重启我的启动应用程序后,这些实体仍然被缓存在Redis我得到一个 org.hibernate.LazyInitializationException

Unfortunately, upon restart of my boot application, the entities are still cached in Redis and I get a org.hibernate.LazyInitializationException

这可能是由于在这篇文章中,即通过hibernate访问分离对象 - 在我的例子中,序列化对象留在缓存中。

This might be for the reason described in this post i.e. access to a detached object by hibernate - in my case the serialized object left around in the cache.

有人可以建议一个处理这个问题的策略吗?

Can someone please advise a strategy for dealing with this problem?


  1. 我应该在销毁应用程序时清理/清空缓存,因为重新缓存缓存的过程非常昂贵,应用程序正在将被托管在云(Heroku)中,其中dynos /容器被销毁并且相当频繁地重新创建(因此重新启动)。
  2. 或者是否有一种方法可以将缓存实体重新附加到实体经理?

  3. 是否有更好的解决方案?
  1. Should I clean/empty the cache upon destroy of my app bearing in mind the process of repopulating the cache is expensive and the app is going to be hosted in the cloud (Heroku) where the dynos/containers are destroyed and recreated (and therefore restarted) quite frequently.
  2. Or is there a way to reattach to the cached entities to the entity manager?
  3. Is there a better solution?


推荐答案

从来没有使用过Redis,但是如果你的缓存应该在应用程序重启时持久存在,你可能需要在缓存实体之前获取所有需要的关系。另一个问题可能是这些缓存的实体是分离的,如果你想在事务中再次使用它们,这可能是一个问题。当然,你可以通过调用 merge(entity)来重新连接它们,而另一方面可能会导致覆盖缓存数据的新数据时出现问题。

Never used Redis, but if your cache is supposed to be persistent across application restarts you might want to fetch all needed relations before caching the entity. One other concern could be that those cached entities are detached, which can be a problem if you want to use them again in transactions. Of course, you might reattach them by calling merge(entity), which on the other hand could cause problems with overriding new data with cached ones.

使用持久缓存的另一个问题是类的版本,因为如果您在redeploys之间更改类,则如果缓存已经填充了以前的类版本的实例,则反序列化将失败。

Another thing to consider with persistent cache is class versions, because if you change your classes between redeploys, deserialization will fail if cache is already populated with instances of previous class version.

这篇关于尝试访问由RedisCacheManager留在Redis中的分离对象时发生LazyInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆