简体   繁体   English

如何从JPA EntityManager清除Hibernate缓存?

[英]How do I clear a Hibernate cache from a JPA EntityManager?

I have an application with entities which Hibernate is caching for me. 我有一个带有Hibernate为我缓存的实体的应用程序。 Works great except that I now I have to clear the cache (due to asynchronous data changes). 除了我现在必须清除缓存(由于异步数据更改)之外,它的工作原理非常好。 Queries are cached as follows: 查询缓存如下:

query.setHint(QueryHints.HINT_CACHEABLE, true);
query.setHint(QueryHints.HINT_CACHE_REGION, "CACHE_REGION_NAME");

The entities have the appropriate annotations (as far as I can tell): 实体具有适当的注释(据我所知):

@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "CACHE_REGION_NAME")

So far so good. 到现在为止还挺好。 Now I need to clear the cache. 现在,我需要清除缓存。 I do the following: 我执行以下操作:

@Override
@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRES_NEW, value = "xProdTransaction")
public void clearClientCache() throws CacheClearingException {
    clearSession();
    clearCacheRegion();
}

void clearSession() throws CacheClearingException {
    Session session = null;
    try {
        session = em.unwrap(Session.class);

        if(session==null) {
            throw new CacheClearingException("Unable to find valid session");
        } else {
            session.clear();
        }
    } catch(PersistenceException pe) {
        throw new CacheClearingException("Unable to clear session", pe);
    }
}

void clearCache() throws CacheClearingException {
    Cache cache = null;
    try {
        SessionFactory sessionFactory = em.unwrap(SessionFactory.class);
        if(sessionFactory == null) {
            throw new CacheClearingException("Unable to find valid session factory");
        }

        cache = sessionFactory.getCache();
        if (cache == null) {
            throw new CacheClearingException("Unable to find valid cache");
        }

        cache.evictCollectionRegion("CACHE_REGION_NAME");
    } catch (PersistenceException pe) {
        throw new CacheClearingException("Unable to clear cache", pe);
    }
}

The session cache is no problem, but I get an exception attempting to unwrap the SessionFactory from the EntityManager: 会话缓存没有问题,但是尝试从EntityManager拆开SessionFactory时出现异常:

com.cambia.shc.core.dao.xproduct.CacheClearingException: Unable to clear cache
    at com.cambia.shc.core.dao.xproduct.CacheManagerImpl.clearCache(CacheManagerImpl.java:63)
    at com.cambia.shc.core.dao.xproduct.CacheManagerImpl.clearClientCache(CacheManagerImpl.java:29)
...
Caused by: javax.persistence.PersistenceException: Hibernate cannot unwrap interface org.hibernate.SessionFactory
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.unwrap(AbstractEntityManagerImpl.java:1524)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291)
    at com.sun.proxy.$Proxy66.unwrap(Unknown Source)
    at      com.cambia.shc.core.dao.xproduct.CacheManagerImpl.clearCache(CacheManagerImpl.java:51)
    ... 94 more

My JPA beans are configured in Spring as follows: 我的JPA Bean在Spring中的配置如下:

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>

<bean id="xproductEntityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="xproduct"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            <prop key="hibernate.generate_statistics">true</prop>
        </props>
    </property>
    <property name="jpaDialect" ref="jpaDialect" />
</bean>

<bean id="xProdTransaction" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="xproductEntityManagerFactory"/>
</bean>

Any ideas? 有任何想法吗?

Thanks! 谢谢!

You cannot get every class directly with unwrap . 您不能直接通过unwrap获得所有课程。 You can get the Session first and then get the SessionFactory from it: 您可以先获取Session ,然后从中获取SessionFactory

em.unwrap(Session.class).getSessionFactory();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM