gpt4 book ai didi

java - 使用 Spring 4 @Transactional + Hibernate 4 + EHCache 忽略缓存

转载 作者:行者123 更新时间:2023-12-01 23:03:32 25 4
gpt4 key购买 nike

几天以来,我在使用 @Transactional 注释的 Spring 上下文中使用 Hibernate 缓存时被阻止了。

我尝试了网上找到的所有解决方案,但没有成功......

唯一可行的解​​决方案是使用 @Cacheable Spring 注释(来自 spring-context-support),但我不满意,因为我无法在我的实体上使用 Hibernate @Cache 注释。@Cacheable 只能用于服务方法等方法,并且我检索没有方法的实体...

例如:

我调用以下服务来获取 CollectionEntity

@Override
@Transactional(readOnly = true)
public CollectionEntity getById(Integer collectionId) throws Exception {
if(collectionId < 1) {
logger.debug("Cannot retrieve a collection from identifier inferior to 1.");
return null;
}
return collectionDao.getById(collectionId);
}

CollectionEntity 包含 ProgramEntity 集

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = CollectionProgram.TABLE, joinColumns = { @JoinColumn(name = COLUMN_COLLECTION_ID, referencedColumnName = CollectionProgram.COLUMN_COLLECTION_ID) }, inverseJoinColumns = { @JoinColumn(name = CollectionProgram.COLUMN_PROGRAM_ID, referencedColumnName = ProgramEntity.COLUMN_PROGRAM_ID) })
private Set<ProgramEntity> programs = new HashSet<ProgramEntity>(0);

这些节目包含一个 ProgramBroadcasting 集

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = ProgramBroadcastingEntity.COLUMN_PROGRAM_ID)
private Set<ProgramBroadcastingEntity> broadcastings = new HashSet<ProgramBroadcastingEntity>(0);

而这些节目广播中包含一个ChannelEntity(一个引用数据)

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = COLUMN_CHANNEL_ID, nullable = false)
private ChannelEntity channel;

因此,如果我想缓存 ChannelEntity,通常只需在其类上添加以下注释即可。

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "Channel")
@Table(name = ChannelEntity.TABLE)
public class ChannelEntity implements java.io.Serializable {

“EAGER”获取对于引用数据来说是一个非常有吸引力的解决方案!但是,如果想使用 @Cacheable(这是目前唯一的解决方案),我必须使用 FetchType.LAZY 声明 ChannelEntity 并编写一个服务类来将 @Cacheable 放在其上,以缓存此数据。这是一个笑话...我不会对所有引用数据类都这样做...

真正的解决方案就是在 ChannelEntity 上添加一个有效的 Hibernate @Cache 注释。

为了使其正常工作,我什至开发了自己的“SingletonEhCacheRegionFactory”类,使用 Spring 类“EhCacheManagerFactoryBean”作为缓存管理器进行初始化。并且继续从数据库中获取数据。该解决方案适用于我的一位同事,但适用于旧版本的 Spring (<4) 和 Hibernate (<4)。所以,对于新版本来说,这似乎不是一个好的解决方案......

所以,我真的需要你的帮助。

在向您提供我的配置之前,这里是我对主类的快速测试,用于从数据库然后从缓存检索 ChannelEntity。

这里测试正常(有效):

private static void testGetChannelFromCache2(BeanFactory factory) throws Exception {
SessionFactoryImpl sessionFactoryImpl = ((SessionFactoryImpl) factory.getBean("sessionFactory"));
Session session = sessionFactoryImpl.openSession();
session.beginTransaction();

ChannelEntity channel1 = (ChannelEntity) session.load(ChannelEntity.class, new Integer(1));
System.out.println(channel1.getLabel());

session.getTransaction().commit();
session.close();

Session anotherSession = sessionFactoryImpl.openSession();
anotherSession.beginTransaction();

// Here I put a breakpoint and I update the value directly on database.

channel1 = (ChannelEntity) anotherSession.load(ChannelEntity.class, new Integer(1));
System.out.println(channel1.getLabel()); // Here I print the cached value, not the new database value. Good!

anotherSession.getTransaction().commit();
anotherSession.close();
}

但这不是真实的上下文。在我的服务层上,我不直接操作事务,我使用了 @Transactional Spring 注释。这是一个更实际的测试:

private static void testGetChannelFromCache1(BeanFactory factory) throws Exception {
ChannelService service = (ChannelService) factory.getBean("channelServiceImpl");
ChannelEntity entity1 = service.getChannelByCode(ChannelCode.ARTE);
if(entity1 != null) {
System.out.println(entity1.getLabel());
}

// Here I put a breakpoint and I update the value directly on database.

ChannelEntity entity2 = service.getChannelByCode(ChannelCode.ARTE);
if(entity2 != null) {
System.out.println(entity2.getLabel()); // Here I print the new database value, not the cached value. Not good...
}
}

这是 ChannelService:

@Service
@Transactional(rollbackFor = Exception.class)
public class ChannelServiceImpl implements ChannelService {

@Log
private Logger logger;

@Inject
private ChannelDao channelDao;

@Override
@Transactional(readOnly = true)
public ChannelEntity getChannelByCode(final ChannelCode code) throws Exception {
if(code == null) {
logger.debug("Cannot find Channel from null code.");
return null;
}
return channelDao.getByCode(code);
}
}

现在,我的配置...

依赖关系:

<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>

<!-- Ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
</dependency>

hibernate 配置:

hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.generate_statistics=true
net.sf.ehcache.configurationResourceName=/config/ehcache/ehcache.xml

EHCache配置:

<cache name="Channel"
maxEntriesLocalHeap="10000"
eternal="true"
overflowToDisk="false">

我使用 Spring 4.0.3.RELEASE、Hibernate 4.3.4.Final 和 EHCache 2.6.8。

更准确地说,@Cache Hibernate 注释似乎有效,但不完全有效......事实上,我在 Hibernate 源代码上放置了几个断点,并且我注意到 Hibernate 将 ChannelEntity 放在缓存上,并且在我第二次调用 ChannelService 后,在缓存上获取并检索 channel 实体!但是...,Hibernate 仍然执行以下数据库请求并检索数据库值。真的很奇怪!

从 Channel this_ 中选择 this_.ChannelId 作为 ChannelI1_3_0_、this_.Code 作为 Code2_3_0_、this_.Label 作为 Label3_3_0_,其中 this_.Code=?

有人对这种奇怪的行为有任何想法吗?

非常感谢您的帮助!

最佳答案

二级缓存和查询缓存有一些问题,看起来很奇怪,但实际上很正常。

例如实体上的@Cache将仅缓存通过其ID加载的实体。如果要缓存除按Id加载之外的查询结果,则需要将查询标记为可缓存:

@NamedQuery(name="account.queryName",
query="select acct from Account ...",
hints={
@QueryHint(name="org.hibernate.cacheable",
value="true")
}
})

或者在条件查询的情况下:

List cats = session.createCriteria(Cat.class)
.setCacheable(true)
.list();

默认情况下,一对多关系不会被缓存。如果你想缓存关联,你需要用@Cache单独标记它们。例如:

@Cache(CacheConcurrencyStrategy.READ_WRITE)
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<ProgramEntity> programs = new HashSet<ProgramEntity>(0);

关于java - 使用 Spring 4 @Transactional + Hibernate 4 + EHCache 忽略缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23174107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com