1

环境 - Grails 2.0.4,Oracle 11G

在几个小时不活动后,从数据库读取会导致java.net.SocketException: Connection timed out。根据其他人关于解决此问题的建议,我使用属性更新数据源以保持数据库连接处于活动状态。这是我看过的几个解决方案-

一种解决方案
另一种解决方案

看来解决方案涉及在数据源上设置适当的值。就我而言,涉及 2 个数据源。我正在尝试进行会话保持活动,以避免连接超时。这似乎是通过设置 validationQuery 属性和 testXXX 属性来完成的。当需要测试时,将运行验证查询。我认为下面的配置会给我想要的结果,但我仍然看到超时。我启动了调试

debug 'org.hibernate'
debug 'jdbc.sqlonly'

我本来希望在某个时间间隔看到validationQuery 运行。根据指定的“6000”毫秒值,我希望每 6 秒看到一次验证查询。这是数据源规范。我尝试了这两种方法。在“properties”闭包中指定它们并将它们指定在与“pooled”相同的级别。

dataSource_secondary {
    logSql = true
    pooled = true
    properties {
        maxActive = 5
        maxIdle = 5
        minIdle = 3
        initialSize = 5
        minEvictableIdleTimeMillis = 6000
        timeBetweenEvictionRunsMillis = 6000
        maxWait = 1000
        testOnBorrow = true
        testWhileIdle = true
        testOnReturn = false
        validationQuery = "select 1 from dual"
    }
}

I also ran this in the debugger and added some code to BootStrap.groovy to get the context.

def ctx = servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT)
println 'Added a println allowing me to set a break point and inspect the context.'

"ctx" did have the expected values as specified in dataSource_secondary. Any ideas why I'm still getting the socket exception?

4

1 回答 1

0

Figured out the problem. Brain dead developer. I neglected to check-in the fix and it didn't make it into the war created by our build environment. Once I checked in the Datasource.groovy, all was groovy and I didn't see the timeout exception this morning. The above solution works.

I'm still confused why I didn't see debug message when the validationQuery ran. Does anyone know the proper log4j entry to see the validationQuery in the logs?

于 2012-08-03T13:36:42.950 回答