簡體   English   中英

如何從JPA EntityManager清除Hibernate緩存?

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

我有一個帶有Hibernate為我緩存的實體的應用程序。 除了我現在必須清除緩存(由於異步數據更改)之外,它的工作原理非常好。 查詢緩存如下:

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

實體具有適當的注釋(據我所知):

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

到現在為止還挺好。 現在,我需要清除緩存。 我執行以下操作:

@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);
    }
}

會話緩存沒有問題,但是嘗試從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

我的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>

有任何想法嗎?

謝謝!

您不能直接通過unwrap獲得所有課程。 您可以先獲取Session ,然后從中獲取SessionFactory

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM