【问题标题】:grails 2.5: "Another unnamed CacheManager already exists in the same VM" when using multiple datasourcesgrails 2.5:使用多个数据源时,“另一个未命名的 CacheManager 已存在于同一 VM 中”
【发布时间】:2015-10-10 01:13:18
【问题描述】:

使用开箱即用的 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'

似乎已经解决了这个问题,但现在的问题是,这个“修复”有什么缺点吗?

【问题讨论】:

    标签: grails datasource ehcache


    【解决方案1】:

    只是为了跟踪(就像 OP 已经在问题本身中回答了一样):

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

    Changes in ehcache version in hibernate plugins

    【讨论】:

    • 应该是 - 'org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory'
    猜你喜欢
    • 2013-03-17
    • 2012-04-18
    • 2012-06-23
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2015-06-29
    • 2016-06-24
    • 1970-01-01
    相关资源
    最近更新 更多