0
我有一個Hibernate爲我緩存的實體應用程序。除非我現在必須清除緩存(由於異步數據更改),所以效果很好。 (據我可以告訴)如何從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豆在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>
任何想法?
謝謝!
謝謝。這很好! – Kramer