试图驱逐二级缓存

时间:2013-12-20 08:18:38

标签: spring hibernate jpa-2.0 second-level-cache evict

我试图在我的持久性实体管理器中逐出二级缓存。看起来我可以使用javax.persistence.Cache接口中定义的以下函数:

/**
 * Clear the cache.
 */
public void evictAll();

为了获取Cache对象,我可以使用javax.persistence.EntityManagerFactory接口中定义的以下函数:

/**
 * Access the cache that is associated with the entity manager
 * factory (the "second level cache").
 * @return instance of the <code>Cache</code> interface
 * @throws IllegalStateException if the entity manager factory
 * has been closed
 *
 * @since Java Persistence 2.0
 */
public Cache getCache();

我连接我的EntityManagerFactory:

@Repository("persistenceManager")
public class PersistenceManager
{
    public EntityManagerFactory emf;

    @PersistenceUnit(unitName="snap")
    public void setEntityManagerFactory(EntityManagerFactory emf) {
        this.emf = emf;
        ...
}

使用以下applicationContext配置文件:

< bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    < property name="persistenceUnitName" value="snap" />
    < property name="persistenceUnitManager" ref="pum" />

当我在对象上调用getCache()时,我得到以下异常:

  

引起:java.lang.AbstractMethodError:   org.hibernate.ejb.EntityManagerFactoryImpl.getCache()Ljavax /持久/缓存;     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)at   org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:376)     在   org.springframework.orm.jpa.AbstractEntityManagerFactoryBean $ ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:517)     在com.sun.proxy。$ Proxy26.getCache(未知来源)at   com.rbccm.gelp.server.util.PersistenceManager.getCache(PersistenceManager.java:30)     在   com.rbccm.gelp.server.service.SystemDataServiceImpl.evictAllSecondLevelCacheEntries(SystemDataServiceImpl.java:133)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)at   org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)     在   org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)     在   org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)     在com.sun.proxy。$ Proxy34.evictAllSecondLevelCacheEntries(未知   来源)at   com.rbccm.gelp.server.SystemGwtServiceImpl.evictAllSecondLevelCacheEntries(SystemGwtServiceImpl.java:132)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)at   com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)

这向我表明getCache(在接口中定义)实际上并未在连接到EntityManagerFactory的对象中实现。所以,我在setEntityManagerFactory(EntityManagerFactory emf)中放了一个breakboint,我注意到对象的具体类型实际上是:org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean

当我浏览这个类的源代码时,我可以确认这里没有实现getCache()。我相信我使用的是正确/兼容的版本:

  • Hibernate 3.6.10
  • Spring 3.1.1
  • JPA 2.0

是否有人遇到任何类似问题或能够重现此问题?我该如何解决这个问题?如果没有,那么从第二级cahce驱逐所有缓存项的替代方法是什么?

1 个答案:

答案 0 :(得分:1)

hibernate有不同的缓存实现。在hibernate属性文件中使用属性hibernate.cache.provider_class来设置具体的缓存类。