Grails 2.5: 在使用多个数据源时出现“同一VM中已存在另一个未命名的CacheManager”

9

在Grails 2.5的开箱即用安装和干净的默认配置下,添加第二个数据源时,在尝试启动应用程序时总是会出现此异常。在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"
    }
}

两个数据库都存在,如果只定义一个数据源,则可以连接到该数据源。

BuildConfig.groovy中包含了默认的所有内容(我假设),包括:

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"
}

有很多帖子出现了这个错误,但似乎是因为作者试图使用非标准版本或缓存。

也尝试按照此帖子将其添加到Config.groovy中:https://github.com/grails/grails-core/releases/tag/v2.5.0

beans {
    cacheManager {
        shared = true
    }
}

很遗憾,这并没有帮助。

请注意,我们正在使用默认的开箱即用配置缓存。

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
}

==== 更新 ====

将以下一行替换(在 DataSource.groovy 文件中的 hibernate 部分):

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

使用这个:

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

看起来已经解决了问题,但现在的问题是,这个“修复”有没有任何副作用?

1个回答

5

仅为了跟踪(就像原始帖子中已经在问题本身中回答的那样):

DataSource.groovy中的cache.region.factory_class更改为以下内容:

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

对于那些遇到错误信息:net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM.的人,请加入以下内容到你的Config.groovy中:

beans {
   cacheManager {
      shared = true
  }
}

请查看Hibernate插件中ehcache版本的更改

2
'org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory' 应该翻译为:单例 EhCache 区域工厂。 - Sudhir N

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接