Setting up ehcache as secondary cache for Hibernate

Hello everyone,

I’m trying to get an application set up on Lucee. This application was originally set up on Cold Fusion. It uses coldspring for dependency injection. It’s configured to use ehcache for secondary cache in Hibernate. I would like some advice on how to get this working. Right now, enabling the secondary cache for Hibernate generates an error message about duplicate cache manager.

Lucee Version: 5.3.7.48
Hibernate Extension: 3.5.5.77

Application.cfc

	this.ormEnabled = true;
	this.ormSettings = {
		flushatrequestend=false,
		eventhandling=true,
		skipCFCWithError=false,
		savemapping=false,
		secondarycacheenabled=true,
		cacheProvider="ehcache",
		cacheconfig='#this.applicationPath#config/ehcache.xml',
...

ehcache.xml

        <defaultCache
        maxElementsInMemory="100000"
        eternal="false"
        timeToIdleSeconds="3600"
        timeToLiveSeconds="3600"
        overflowToDisk="false"
        diskSpoolBufferSizeMB="30"
        maxElementsOnDisk="10000000"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="1800"
        memoryStoreEvictionPolicy="LRU"
        clearOnFlush="true"
        statistics="true"
            />

        <cache
        name="myCache"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="360"
        timeToLiveSeconds="360"
        overflowToDisk="false"
        diskSpoolBufferSizeMB="30"
        maxElementsOnDisk="10000000"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="180"
        memoryStoreEvictionPolicy="LRU"
        clearOnFlush="true"
        statistics="true"
            />
</ehcache>

Below is the error I get.

Message string net.sf.ehcache.CacheException: Another CacheManager with same name 'app_extg50k1qvnjyre' 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: URLConfigurationSource [url=file:/C:/Lucee/tomcat/temp/ehcache/app_extg50k1qvnjyre.xml]
StackTrace string lucee.runtime.exp.NativeException: net.sf.ehcache.CacheException: Another CacheManager with same name 'app_ext50k1qvnjyre' 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: URLConfigurationSource [url=file:/C:/Lucee/tomcat/temp/ehcache/app_extg50k1qvnjyre.xml]
at net.sf.ehcache.hibernate.EhCacheRegionFactory.start(EhCacheRegionFactory.java:89)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:250)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
at org.lucee.extension.orm.hibernate.SessionFactoryData.buildSessionFactory(SessionFactoryData.java:208)
at org.lucee.extension.orm.hibernate.HibernateORMEngine.getSessionFactoryData(HibernateORMEngine.java:202)
at org.lucee.extension.orm.hibernate.HibernateORMEngine.create(HibernateORMEngine.java:452)
at org.lucee.extension.orm.hibernate.HibernateORMSession.load(HibernateORMSession.java:644)
at org.lucee.extension.orm.hibernate.HibernateORMSession.load(HibernateORMSession.java:638)
at lucee.runtime.functions.orm.EntityLoad.call(EntityLoad.java:50)
at common.server.security.securityservice_cfc$cf.udfCall1(/ems/common/server/security/SecurityService.cfc:29)
at common.server.security.securityservice_cfc$cf.udfCall(/ems/common/server/security/SecurityService.cfc)
at lucee.runtime.type.UDFImpl.implementation(UDFImpl.java:106)
at lucee.runtime.type.UDFImpl._call(UDFImpl.java:344)
at lucee.runtime.type.UDFImpl.callWithNamedValues(UDFImpl.java:207)
at lucee.runtime.ComponentImpl._call(ComponentImpl.java:647)
at lucee.runtime.ComponentImpl._call(ComponentImpl.java:568)
at lucee.runtime.ComponentImpl.callWithNamedValues(ComponentImpl.java:1917)
at lucee.runtime.util.VariableUtilImpl.callFunctionWithNamedValues(VariableUtilImpl.java:864)
at lucee.runtime.PageContextImpl.getFunctionWithNamedValues(PageContextImpl.java:1729)
at beans.support.cfcbeandefinition_cfc$cf.udfCall(/coldspring/beans/support/CFCBeanDefinition.cfc:40)
at lucee.runtime.type.UDFImpl.implementation(UDFImpl.java:106)
at lucee.runtime.type.UDFImpl._call(UDFImpl.java:344)
at lucee.runtime.type.UDFImpl.call(UDFImpl.java:217)
at lucee.runtime.type.scope.UndefinedImpl.call(UndefinedImpl.java:779)
at lucee.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:785)
at lucee.runtime.PageContextImpl.getFunction(PageContextImpl.java:1710)
at beans.support.abstractbeandefinition_cfc$cf.udfCall1(/coldspring/beans/support/AbstractBeanDefinition.cfc:59)
at beans.support.abstractbeandefinition_cfc$cf.udfCall(/coldspring/beans/support/AbstractBeanDefinition.cfc)
at lucee.runtime.type.UDFImpl.implementation(UDFImpl.java:106)
at lucee.runtime.type.UDFImpl._call(UDFImpl.java:344)
at lucee.runtime.type.UDFImpl.call(UDFImpl.java:217)
at lucee.runtime.ComponentImpl._call(ComponentImpl.java:646)
at lucee.runtime.ComponentImpl._call(ComponentI
-- very long stack, omitted 72376 bytes

The issue had apparently been resolved in Lucee 4.5.5.x. See [LDEV-1855] - Lucee

Hi, we are facing the same problem right now. We are also moving our applications from ColdFusion to Lucee. Where you able to solve this issue?

which version of Lucee?

for ORM, try the 3.5.5.87-SNAPSHOT

I’m using the latest 5.3.9 release version
Hibernate is using 3.5.5.84. I’ll try the suggested snapshot.

Updating to the latest 3.x snapshot does nog solve the problem.

Can you share your code?

I’m not sure what part of the code you want me to share. But in the application.cfc we have this setting:

this.ormSettings={
			//saveMapping="true",
			datasource: "dsn_studiemeter7",
			cfcLocation: [this.appRoot & "nl/xxxxxxx/xxxxxxxx/orm"],
			secondaryCacheEnabled: true,
			cacheProvider: "ehcache",
			eventHandling: true,
			logSql: false,
			dialect: "MicrosoftSQLServer"
		};

We also tried to add our own EHCache config with:

cacheConfig: this.appRoot & "../HibernateEHCache.xml",

By the way, it happens the second (and further) time we request any template. The first time any template loads fine.

I found something very interesting. We have two applications that are almost similar and share a lot of the codebase. Only one of them has this problem.
Their application.cfc is identical (besides the application names of course) except one setting.
In the failing application we have a restInitApplication() that configures the REST api. As soon as I remove that line of code, the duplicate cachemanager error disappears.
What I don’t understand is how this function is in any way related to the secondary cache of ORM.

here are some examples?

@dik_deviant I replicate the issue by using the restInitApplication().
Are you using serviceMapping attribute in restInitApplication()? if yes, please check your Application name and serviceMapping attribute name are same value.
The Application name and serviceMapping attribute name both have different value or differences in the case(ex: appname/appNAME) means it throws the same error

I’m using Lucee 5.3.10.120 with Hibernate 3.5.5.87 installed. If I set secondarycacheenabled=true, I get the same error message “Another CacheManager with same name ‘XXX’ already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following”

Any chance this bug has resurfaced after it was fixed in 3.5.5.87 version?

FYI, I do have root/Application.cfc that intializes ORM and a root/api/Application.cfc that initializes ORM also. I imagine that may be a reason for the error in my code but how can I check to see if the ORM settings are already intialized so prevent re-initialization?