首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

转载-grails的dbcp连接池链接失效有关问题的解决方案

2012-09-07 
转载--grails的dbcp连接池链接失效问题的解决方案}This seems to have solved the problem for me. Since

转载--grails的dbcp连接池链接失效问题的解决方案

}

This seems to have solved the problem for me. Since my firewall was dropping the socket connections at 60 minutes, all I did was proactively run the idle object evictor every half 30 minutes, flush connections that are idle for more than 30 minutes and regenerate new connections in the pool. I also did sanity check over the connections in the pool.

1 楼 jackle_liu 2012-06-25   转载---对于grails版本在1.3以上的解决方案
Grails turns database connection pooling on by default, and uses the Apache Commons DBCP library to do the pooling.

With many of the popular JDBC drivers, this can cause a failure when the application is accessed after a long idle time, commonly when a business application sits unused overnight, and then is accessed first-thing in the morning. The first user in the morning gets an HTTP 500 error, and then for subsequent users, everything is ok.

There are several ways to solve this, including changing to a different connection pool, but Sudarshan Acharya has a blog post from 2009 that explains a fairly easy way to configure DBCP to resolve this issue. Of course, there’s a “gotcha” here, or I wouldn’t be blogging about it:

As Tom Eastman points out, if you use Sudarshan’s example with Grails 1.3.x, you get an exception something like:

ERROR context.GrailsContextLoader – Error executing bootstraps: groovy.lang.MissingMethodException: No signature of method: org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy.setMinEvictableIdleTimeMillis() is applicable for argument types: (java.lang.Integer) values: [900000]
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy.setMinEvictableIdleTimeMillis() is applicable for argument types: (java.lang.Integer) values: [900000]

As Graeme Rocher helpfully points out, this is because the properties of org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy changed with Grails 1.3.0, and you must now set the properties on ‘dataSourceUnproxied’ instead of setting them on ‘dataSource’. With this change, everything works as expected.

So, here’s an example of the new configuration of BootStrap.groovy, with Graeme’s suggested change:
view source
print?
01.def ctx = servletContext.getAttribute(
02.ApplicationAttributes.APPLICATION_CONTEXT
03.)
04.def dataSource = ctx.dataSourceUnproxied
05.
06.println "configuring database connection pool"
07.
08.dataSource.setMinEvictableIdleTimeMillis(1000 * 60 * 30)
09.dataSource.setTimeBetweenEvictionRunsMillis(1000 * 60 * 30)
10.dataSource.setNumTestsPerEvictionRun(3)
11.dataSource.setTestOnBorrow(true)
12.dataSource.setTestWhileIdle(false)
13.dataSource.setTestOnReturn(false)
14.dataSource.setValidationQuery("SELECT 1")
15.
16.dataSource.properties.each { println it }

热点排行