Getting Grails Database Connections to Reconnect

Where I work, a bunch of the Grails have many, many datasources. One app in particular I was brought in on because it was dropping a bunch of connections on Saturday night or Sunday morning. The app had to be restarted every Sunday morning and no one was sure why. They said that database had stayed up and one datasource was fine.

Once upon a time I used BoneCP to fix a similar problem in a Java app but a little research showed that it is no longer maintained. I asked around and was pointed to HikariCP as a good replacement.

But of course I had to re-create the original problem first. I changed the one of the datasources to be at localhost:3000 and the setup an ssh tunnel from my port 3000 to the actual datasource. I did a run-app and tested to make sure things were fine. And then I killed the ssh connection and I got connection errors — as expected. I opened the ssh connection again while the app was up — same connection error. What I wanted it to do is to reconnect.

So I hunted down how to put HikariCP in Grails. Luckily someone had already done it for me and so I put it into my app. Note that you must have pooled = false in your DataSource.groovy file for all the extra datasources because you don’t want Grails’ default pooling to go — you want it to be controlled by HikariCP.

After I got HikariCP configured in the application, I redid my test… and, sure enough, when the ssh tunnel was back up, the connection was live. This should solve the Sunday Morning Problem.

About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

One thought on “Getting Grails Database Connections to Reconnect

  1. ludo_rj says:

    Without loading any dependencies, url = “jdbc:mysql://localhost/?autoReconnect=true” works like a charm.

  2. Scott says:

    I’m trying the same thing but getting an error when trying to load an Oracle driver. Since Oracle is not on Maven, I have it in my Grails /lib directory. It seems that it’s not available by the time the resources.groovy try to instantiate the driver as I get a class name not found. The Oracle driver works fine when using the standard datasource setup, but not when trying to change the datasource within the resources.groovy. Any idea why this might be occurring? Does this solution still work for you if you move your driver from a Maven dependency to the /lib directory?

  3. Matt says:

    I didn’t think there was any way for the default Grails database pool to reconnect, but thanks to this post, I’ve been corrected: http://stackoverflow.com/questions/31881250/heroku-postgres-this-connection-has-been-closed

    Adding the following properties in DataSource.groovy got this to work for me.

    dataSource {
    pooled = true
    properties {
    testOnBorrow = true
    testWhileIdle = true
    testOnReturn = true
    validationQuery = “SELECT 1”
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Android Development for iOS Developers
Android development has greatly improved since the early days. Maybe you tried it out when Android development was done in Eclipse, emulators were slow and buggy, and Java was the required language. Things have changed […]
Add a custom object to your Liquibase diff
Adding a custom object to your liquibase diff is a pretty simple two step process. Create an implementation of DatabaseObject Create an implementation of SnapshotGenerator In my case I wanted to add tracking of Stored […]
Keeping Secrets Out of Terraform State
There are many instances where you will want to create resources via Terraform with secrets that you just don’t want anyone to see. These could be IAM credentials, certificates, RDS DB credentials, etc. One problem […]
Validating Terraform Plans using Open Policy Agent
When developing infrastructure as code using terraform, it can be difficult to test and validate changes without executing the code against a real environment. The feedback loop between writing a line of code and understanding […]