手动更新数据库后清除Hibernate二级缓存 [英] Clear Hibernate 2nd level cache after manually DB update

查看:825
本文介绍了手动更新数据库后清除Hibernate二级缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很快,我有一个实体映射到在DB(Oracle)中使用启用的第二级缓存(只读策略)查看 - ehcache。



如果手动更新DB缓存中的某些列将不会更新。



我没有找到任何方法来执行此操作。只有当更新通过Hibernate实体完成。



我可以以某种方式实现此功能吗?

也许工作监视表(或视图)?或者,也许有一些方法可以通知Hibernate有关具体表格中数据库的更改。



感谢您的未来答案!

解决方案

根据 Hibernate JavaDoc ,您可以使用 org.hibernate.Cache.evictAllRegions()


evictAllRegions()从缓存中取出所有数据。

使用Session和SessionFactory:

  Session session = sessionFactory.getCurrentSession(); 

if(session!= null){
session.clear(); //内部缓存清除
}

缓存缓存= sessionFactory.getCache();

if(cache!= null){
cache.evictAllRegions(); //驱逐所有查询区域的数据。





1)如果你只需要更新一个实体(如果直接从db中获取,可以使用
$ b


evictEntityRegion(Class entityClass)从给定区域中退出所有实体数据(即



2)如果你有很多实体,可以直接从db中更新,你可以使用这种方法从第二个(我们可以通过JMX或其他管理工具将此方法公开给管理员):

  / ** 
* Evicts所有二级缓存休眠条件。当外部应用程序修改游戏数据库时,通常只需
*。
* /
public void evict2ndLevelCache(){
try {
Map< String,ClassMetadata> classesMetadata = sessionFactory.getAllClassMetadata();
Cache cache = sessionFactory.getCache();
for(String entityName:classesMetadata.keySet()){
logger.info(Evicting Entity from 2nd level cache:+ entityName);
cache.evictEntityRegion(entityName);

} catch(Exception e){
logger.logp(Level.SEVERE,SessionController,evict2ndLevelCache,Error evicting 2nd level hibernate cache entities:,e);






3)这里描述的另一个apporche 这里为postgresql +休眠,我觉得你可以做类似Oracle 像这样


Shortly, I have an entity mapped to view in DB (Oracle) with enabled 2nd level Cache (read only strategy) -- ehcache.

If I manually update some column in DB -- cache will not be updated.

I did not find any ways to do this. Only if updates will be done through Hibernate entity.

May I somehow implement this feature?

Maybe Job to monitor table (or view)? Or maybe there is some method to notify Hibernate about change in DB in concrete table.

Thanks for future answers!

解决方案

According to Hibernate JavaDoc, you can use org.hibernate.Cache.evictAllRegions() :

evictAllRegions() Evict all data from the cache.

Using Session and SessionFactory:

Session session = sessionFactory.getCurrentSession();

if (session != null) {
    session.clear(); // internal cache clear
}

Cache cache = sessionFactory.getCache();

if (cache != null) {
    cache.evictAllRegions(); // Evict data from all query regions.
}

1) If you need update only one entity (if directly from db you will update only certain entities) not whole session, you can use

evictEntityRegion(Class entityClass) Evicts all entity data from the given region (i.e.

2) If you have a lot of entities, that can be updated directly from db you can use this method that evicts all entities from 2nd level cache (we can expose this method to admins through JMX or other admin tools):

/**
 * Evicts all second level cache hibernate entites. This is generally only
 * needed when an external application modifies the game databaase.
 */
public void evict2ndLevelCache() {
    try {
        Map<String, ClassMetadata> classesMetadata = sessionFactory.getAllClassMetadata();
        Cache cache = sessionFactory.getCache();
        for (String entityName : classesMetadata.keySet()) {
            logger.info("Evicting Entity from 2nd level cache: " + entityName);
            cache.evictEntityRegion(entityName);
        }
    } catch (Exception e) {
        logger.logp(Level.SEVERE, "SessionController", "evict2ndLevelCache", "Error evicting 2nd level hibernate cache entities: ", e);
    }
}

3) here another apporche described here for postgresql+hibernate, I think similar you can do for Oracle like this

这篇关于手动更新数据库后清除Hibernate二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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