【发布时间】:2014-01-02 17:43:45
【问题描述】:
我们正在使用 Grails 2.0.4、GORM 和 Hibernate 构建应用程序。当数据库不可用时,Grails 不会初始化,启动失败。我们认为我们的池设置可以防止启动失败,但事实并非如此。
如果单独的池设置不能解决这个问题,是否可以在resources.groovy 中捕获异常,如果无法初始化数据库服务,则暂时切换到基于文件的服务?像这样的...
resources.groovy
try{
myDataService(PostgresDatabaseServiceImpl){}
}catch(Exception e){
//if database connect failed, use local service instead
myDataService(FileBasedServiceImpl){}
}
即使以上是可能的,它也会产生一个新的问题;一旦数据库可用,如何动态切换回来。我们尝试了上面的 try/catch,但没有任何影响,启动问题仍然存在:
创建名为“transactionManagerPostProcessor”的 bean 时出错: bean初始化失败
如果仅通过池设置就可以避免启动失败,我们当然可以在应用程序尝试使用错误的数据库连接时在运行时管理 SQL 异常,但我们无法管理启动失败。
DataSource.groovy(池设置)
dataSource {
pooled = true
driverClassName = "org.postgresql.Driver"
properties {
maxActive = 20
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
hibernate {
cache.use_second_level_cache = false
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
【问题讨论】:
标签: java spring hibernate grails grails-orm