日期:2014-05-16  浏览次数:20497 次

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

Grails, DBCP & Stale Connections

July 13, 2009


Does your application break after every few hours of inactivity even though you have enough database connections in the pool? It is a common problem with database connection pooling (and idle sockets connections in general).

I have been running a few Grails app with PostgreSQL database using Apache Commons DBCP connection pooling. Most of these apps are pretty busy, and are working quite well so far. But I have one critical app that doesn’t get used as much. Recently after a bug report, I was watching through the logs and I realized that this app was complaining about Socket Connection Exception after every hour of idle time. Try it again, and it would work. So why was it rejecting the DB connection the first time? Creepy!

I checked out the other apps I have, and all of them were suffering from the same problem - depending on how idle they were. I couldn’t ignore any longer.

I started off with the basics.

Basic configuration:

I have my datasource defined in the DataSource.groovy file under grails-app/conf. I have enabled connection pooling and I am using the appropriate PostgreSQL-JDBC driver. Grails comes with Apache DBCP connection pooling by default. So it just works.

01. environments {
02. production {
03. dataSource {
04. pooled = true
05. driverClassName = "org.postgresql.Driver"
06. dbCreate = "update"
07. url = "jdbc:postgresql://sacharya.com:5432/sacharya_prod_db"
08. username = "myuser"
09. password = "mypassword"
10. logSql=true
11. }
12. }