Question

I'm using SecondLevelCache with hibernate. This is my xml configuration file:

    <persistence-unit name="EntityTestHibernate" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/DB_NAME"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.username" value="USERNAME"/>
            <property name="hibernate.connection.password" value="PASSWORD"/>            
            <property name="hibernate.cache.use_second_level_cache" value="true"/>             
            <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />


My ehcache.xml:

<ehcache name="cacheTest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
    <cache  name="entityCache" 
            maxEntriesLocalHeap="50"
            eternal="false"
            timeToLiveSeconds="120"         
    />
</ehcache>

and on my entity there is an annotation like this

@Cache(region="entityCache", usage=CacheConcurrencyStrategy.READ_WRITE )

When i run locally my UnitTest i have no problems. All tests passed, but if i run tests on my Hudson continuos integretion i have following error (problem is same if i set or not annotation on it @DirtiesContext):

>     javax.persistence.PersistenceException: org.hibernate.cache.CacheException: java.lang.IllegalStateException:
> The cacheTest Cache is not alive (STATUS_SHUTDOWN)    at
> org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1167)
>   at
> org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:673)
>   at
> org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:641)
> Caused by: org.hibernate.cache.CacheException:
> java.lang.IllegalStateException: The cacheTest Cache is not alive
> (STATUS_SHUTDOWN)     at
> net.sf.ehcache.hibernate.regions.EhcacheTransactionalDataRegion.remove(EhcacheTransactionalDataRegion.java:164)
>   at
> net.sf.ehcache.hibernate.strategy.AbstractEhcacheAccessStrategy.evict(AbstractEhcacheAccessStrategy.java:119)
>   at
> net.sf.ehcache.hibernate.nonstop.NonstopAwareEntityRegionAccessStrategy.evict(NonstopAwareEntityRegionAccessStrategy.java:96)
>   at
> org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:144)
>   at
> org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:62)
>   at org.hibernate.impl.SessionImpl.fireRefresh(SessionImpl.java:1118)
>   at org.hibernate.impl.SessionImpl.refresh(SessionImpl.java:1098)    at
> org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:666)
> Caused by: java.lang.IllegalStateException: The cacheTest Cache is
> not alive (STATUS_SHUTDOWN)   at
> net.sf.ehcache.Cache$CacheStatus.checkAlive(Cache.java:3946)  at
> net.sf.ehcache.Cache.checkStatus(Cache.java:2664)     at
> net.sf.ehcache.Cache.removeInternal(Cache.java:2288)  at
> net.sf.ehcache.Cache.remove(Cache.java:2207)  at
> net.sf.ehcache.Cache.remove(Cache.java:2125)  at
> net.sf.ehcache.hibernate.regions.EhcacheTransactionalDataRegion.remove(EhcacheTransactionalDataRegion.java:160)

How could i solve that problem? Why that happens only on Hudson?


UPDATE

As suggested by @lanimall i have:

  • disabled all nodes on Hudson;
  • modified persistence.xml.

Replacing

< property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />

with

<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory" />

but that doesn't solve my problem (but it's a good starting :-)...error is same).

My environment configuration is hibernate3 and ehcache-2.7.2

Was it helpful?

Solution 2

I have found solution!

In my persistence.xml i had 2 persistence-unit configured as Singleton and in some tests were used both. In that way those share EhCacheSingleton.

Then happens that when EntityFactory associated to one of that was shutdown, consequently make a ehcache shutdown that was used by the other one (because there is one instance of Singleton shared between those 2 entity manager).

Many thanks to @lanimall to open my mind!

OTHER TIPS

what version of hibernate are you using? and Ehcache? I think what could be happening is that hudson is running on some application server (such as Glassfish, Tomcat 5, JBoss, Jetty 6, etc) and it might have either some ehcache or hibernate lib versions that are competing with the ones you're embedding in your app...hence the problem happening only when running tests in hudson. Make sure to look into that...

Also, per ehcache/hibernate documentation (http://ehcache.org/documentation/user-guide/hibernate#Configure-Ehcache-as-the-Second-Level-Cache-Provider), please try to use the "hibernate.cache.region.factory_class" setting as opposed to the "hibernate.cache.provider_class"...

And I also usually use the singleton factory (otherwise your JUNIT tests will not work...). Based on hibernate version, use hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory

or (for hibernate 4.x and above)

hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory

hope that helps...

JUnit share Spring context for speed. I've avoid from this exception when remove explicitly Spring context closing in one of my test.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top