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
Planning an Apache Airflow Deployment
This is part one of a five-part series addressing Airflow at an enterprise scale. I will update these with links as they are published. Airflow: Planning a Deployment Apache Airflow is a platform for authoring, […]
Rockstar Development
This originally appeared on Marty Henderson’s personal blog Or, how to use Gitpod and GitLab so that no one else has to care about your questionable coding language choices. A true rockstar has a good […]
Testing a Quarkus Kafka Application
Quarkus, a “Kubernetes Native Java stack,” enables lighter Java applications with faster startup times. In a recent post, I talked about scaling Kafka consumers in Kubernetes. Quarkus applications fit right into this picture because they […]
Gitpod and Hringvegurinn
Iceland Ever seen an advertisement for visiting Iceland? Have you noticed that they all mention Hringvegurinn or the Ring Road, as a good tour? (If you haven’t seen a tour ad for Iceland, Steindi Jr […]