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.

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:

    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 *