Skip to content

Instantly share code, notes, and snippets.

@squarepegsys
Created August 19, 2015 00:43
Show Gist options
  • Save squarepegsys/85a0fe380ab231391b8f to your computer and use it in GitHub Desktop.
Save squarepegsys/85a0fe380ab231391b8f to your computer and use it in GitHub Desktop.
HikariCP in Grails
// Shameless stolen from http://stackoverflow.com/questions/25772324/defining-an-alternate-connection-pool-in-grails-2-3-6
def config = Holders.config
def dataSources = config.findAll {
it.key.toString().contains("dataSource_")
}
dataSources.each { key, value ->
def ds = value
"${key}"(HikariDataSource, { bean ->
def hp = new Properties()
hp.username = ds.username
hp.password = ds.password
hp.connectionTimeout = 6000
hp.maximumPoolSize = 60
hp.jdbcUrl = ds.url
hp.driverClassName = ds.driverClassName
HikariConfig hc = new HikariConfig(hp)
bean.constructorArgs = [hc]
})
}
@abhilasha24
Copy link

abhilasha24 commented Mar 23, 2017

Hi,
My grails application is not able to pick configuration changes for Hikari. That is how my resources.groovy looks like

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
beans = {
    def config = application.config
    println config
    def dataSources = config.findAll {
        println it.key.toString()
        it.key.toString().contains("dataSource")
    }
    dataSources.each { key, value ->
        println key + value
        def ds = value
        "${key}"(HikariDataSource, { bean ->

            def hp = new Properties()
            hp.username = ds.username
            hp.password = ds.password
            hp.jdbcUrl = ds.url
            hp.driverClassName = ds.driverClassName
            hp.connectionTimeout = 1000
            hp.idleTimeout = 120000
            hp.leakDetectionThreshold = 15000
            hp.maxLifetime = 1800000l
            hp.maximumPoolSize = 3
            hp.minimumIdle = 1
            hp.validationTimeout = 1000
            hp.connectionTestQuery = "SELECT 1"

            HikariConfig hc = new HikariConfig(hp)
            bean.constructorArgs = [hc]
        })
    }
}

and have my application.groovy as

environments {
    development {
        dataSource {
            dbCreate = ""
            pooled = true
            driverClassName = "org.postgresql.Driver"
            dialect = "net.kaleidos.hibernate.PostgresqlExtensionsDialect"
            url = "jdbc:postgresql://localhost:5432/postgres"
            username = "postgres"
           // password = "!"
            logSql = true
        }
    }
}

That's my structure
screenshot from 2017-03-23 14-42-17

Am I missing any other configuration?

@zatziky
Copy link

zatziky commented Jan 5, 2018

@abhilasha24 You have to set pooled = false so that hikari can handle the pooling.

@vikaschauhan17
Copy link

Is it working now @abhilasha24 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment