Trying to evict second level cache

743 views Asked by At

I am trying to evict the second level cache in my persistence entity manager. Looks like I can use the following function defined in the javax.persistence.Cache interface:

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

And in order to get the Cache object, I can use the following function defined in the javax.persistence.EntityManagerFactory interface:

/**
 * 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();

I wire my EntityManagerFactory as such:

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

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

With the following applicationContext configuration file:

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

When I call getCache() on the object, I get the following exception:

Caused by: java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerFactoryImpl.getCache()Ljavax/persistence/Cache; at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:376) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:517) at com.sun.proxy.$Proxy26.getCache(Unknown Source) at com.rbccm.gelp.server.util.PersistenceManager.getCache(PersistenceManager.java:30) at 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at com.sun.proxy.$Proxy34.evictAllSecondLevelCacheEntries(Unknown Source) 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)

This indicates to me that getCache (defined in the interface) is not actually implemented in the object that is being wired into EntityManagerFactory. So, I put a breakboint in setEntityManagerFactory(EntityManagerFactory emf) and I noticed the concrete type of the object is in fact: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean

When I browse the source code of this class, I can confirm that getCache() is not implemented here. I believe I am using the correct/compatible versions:

  • Hibernate 3.6.10
  • Spring 3.1.1
  • JPA 2.0

Has anyone come across any similar issues or is able to reproduce this problem? How do I address this? If not, then what is an alternative to evict all cache items from second level cahce?

1

There are 1 answers

1
Fireworks On BEST ANSWER

hibernate has different cache realization. In hibernate property file use property hibernate.cache.provider_class to set concrete cache class.