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

Object Partners profile.

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 to Scott Cancel reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]