playframework / play1

Play framework
https://www.playframework.com/documentation/1.4.x/home
Other
1.58k stars 684 forks source link

Can't use hibernate second level cache in 1.4.4 #1117

Open dabeeeenster opened 7 years ago

dabeeeenster commented 7 years ago

Referencing #952 I've tried to get this working in 1.4.4 but am getting errors. If I add this to my application.conf

hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.EhCacheRegionFactory

I get the following error:

Oops: PersistenceException
Unexpected error : Unexpected Error, caused by exception PersistenceException: Unable to build EntityManagerFactory

play.exceptions.UnexpectedException: Unexpected Error
    at play.Play.start(Play.java:571)
    at play.Play.detectChanges(Play.java:659)
    at play.Invoker$Invocation.init(Invoker.java:209)
    at Invocation.HTTP Request(Play!)
Caused by: javax.persistence.PersistenceException: Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:925)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:900)
    at play.db.jpa.JPAPlugin.onApplicationStart(JPAPlugin.java:198)
    at play.plugins.PluginCollection.onApplicationStart(PluginCollection.java:616)
    at play.Play.start(Play.java:541)
    ... 3 more
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:211)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:175)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:156)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:267)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1799)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:918)
    ... 7 more
Caused by: org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:110)
    at org.hibernate.internal.CacheImpl.<init>(CacheImpl.java:70)
    at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:40)
    at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:35)
    at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:91)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:201)
    ... 13 more
Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
    at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:529)
    at net.sf.ehcache.CacheManager.init(CacheManager.java:374)
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:259)
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:83)
    ... 18 more

I noticed that the package name for EhCacheRegionFactory in the newly included jar was org.hibernate.cache.ehcache.EhCacheRegionFactory but setting that in the application.conf also errors. Is anyone else able to get this working?

elar1000 commented 7 years ago

You have to use newer package for sessionFactory as described here http://www.ehcache.org/documentation/2.7/integrations/hibernate.html

I got something working with putting hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory

But I am not sure that using Singelton factory class is actually the best idea

Also got it working with hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

For this I added custom cache configuration file and hibernate.properties file to app directory. In hibernate.properties files I specified the custom cache configuration location net.sf.ehcache.configurationResourceName=../conf/myehcache.xml