Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails 2.5: "Another unnamed CacheManager already exists in the same VM" when using multiple datasources

With an out of the box installation of Grails 2.5 and a clean default, config, adding a second datasource always gives this exception when trying to start the app. This used to work no problem with grails 2.3.x

DataSource.groovy:

environments {
  development {
    dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://127.0.0.1:3306/myapp"
        username = "myuser"
        password = "mypass"
    }

    dataSource_report {
       url = "jdbc:mysql://127.0.0.1:3306/myapp_reporting"
       username = "someuser"
       password = "somepass"
    }
}

Both databases exist, and can be connected to if only one datasource defined.

In the BuildConfig.groovy, is all the stuff which came as default (I assume), including:

plugins {
    build ":tomcat:7.0.55"

    compile ":scaffolding:2.1.2"
    compile ':cache:1.1.8'
    compile ":asset-pipeline:2.1.1"
    compile ":spring-security-core:2.0-RC4"
    compile ":quartz:1.0.2"

    runtime ":hibernate4:4.3.8.1" // or ":hibernate:3.6.10.18"
    runtime ":database-migration:1.4.0"
    runtime ":cors:1.1.6"
}

There are many posts with this error, but they seem to be because the author is attempting to use non standard versions or caching.

Also tried adding this to Config.groovy, as per this post: https://github.com/grails/grails-core/releases/tag/v2.5.0

beans {
    cacheManager {
        shared = true
    }
}

This did not help, unfortunately.

Note, we are using the default out of the box configured caching

hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
    singleSession = true // configure OSIV singleSession mode
    flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}

==== UPDATE ====

Replacing this line (in DataSource.groovy under hibernate section):

cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' 

With this one:

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

Seems to have fixed the issue, but the question now is, are there any downsides to this "fix"?

like image 658
John Little Avatar asked Jul 20 '15 19:07

John Little


1 Answers

Just to keep track (like the OP has already answered in the question itself):

Change the cache.region.factory_class in DataSource.groovy to like this:

hibernate {
    cache.region.factory_class = "org.hibernate.cache.SingletonEhCacheRegionFactory"
}

And for those who are getting error like: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM., add following to your Config.groovy

beans {
   cacheManager {
      shared = true
  }
}

See Changes in ehcache version in hibernate plugins

like image 67
Shashank Agrawal Avatar answered Sep 29 '22 02:09

Shashank Agrawal