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
Rewriting files in Google Cloud Storage
Rewriting Files in GCP Note: even though this code is in Python, this should be the same idea in JavaScript, Go, etc. I wrote the following to copy a file from one Google Cloud Storage […]
Interpreting Spatial Data in the Age of COVID-19
As 2020 has come to an end, many are eager to leave the mess of COVID-19 behind with the new year and gain a fresh start. Unfortunately, new cases are still soaring across the United […]
Building a Better Mousetrap
Recently, my daughter (age 6) was into building “mousetraps” out of shoe boxes. These were more or less comfortable cardboard mouse houses complete with beds, rooms, everything a mouse could want or need and not […]
ARM Wrestling Its Way Into Mainstream Software Development
Nearly all smart phones have been running ARM-based processors for years. They provide superior power for the amount of power consumed, and thus extend battery life. With Apple’s recent release of the Apple Silicon M1 […]