Setting HikariConfig and getting IllegalStateException

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Setting HikariConfig and getting IllegalStateException

PL99
Hi, all!
I am novice in Ratpack and I am trying creating application, using HikariCP.
When I tried using datasource.properties file for setting ServerConfig object and then use it in HikariConfig, I was getting java.lang.IllegalStateException: Failed to build required config items

datasource.properties content is here:
datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.jdbcUrl = "jdbc:oracle:thin:@127.0.0.1:1521:MY_XE"
datasource.username = PL99
datasource.password = PWD
datasource.connectionTestQuery=select 1 from dual
My code Ratpack.groovy is here
ratpack {

    serverConfig {
        ServerConfigBuilder config ->
            config
                    .baseDir(BaseDir.find())
                    .props("config.properties")
                    .props("datasource.properties")
                    .env()
                    .sysProps()
                    .args(programArgs.stream().toArray() as String[])
                    .require("/metrics", DropwizardMetricsConfig)
                    .require("/datasource", HikariConfig) //  java.lang.IllegalStateException here!
    }

    bindings {
        module MarkupTemplateModule
        module SessionModule

    }
   handlers {
        get {
println serverConfig.properties
            render groovyMarkupTemplate("index.gtpl", title: "My Ratpack App")
        }
}


I was reading topic https://forum.ratpack.io/Hikari-Configuration-File-tt841.html and understood that I must set HikariCP properties using serverConfig after creation it.

But in https://github.com/ratpack/example-books/blob/master/src/ratpack/ratpack.groovy I am see that some classes i can set in serverConfig section
.require("/isbndb", IsbndbConfig)
.require("/metrics", DropwizardMetricsConfig)

Why this method not working with HikariConfig class? May be I need change datasource.properties file?
Reply | Threaded
Open this post in threaded view
|

Re: Setting HikariConfig and getting IllegalStateException

PL99
This code is working

File JdbcCfg.groovy
import groovy.transform.CompileStatic
/**
 * Created by PL99 on 17.04.2016.
 */
@CompileStatic
class JdbcCfg {
    String driverClassName = "org.h2.jdbcx.JdbcDataSource"
    String connectionTestQuery
    String username
    String password
    String jdbcUrl
    Integer maximumPoolSize = 5
    Integer connectionTimeout = 30000
    Integer maxLifetime = 180000
    String poolName
}

Ratpack.groovy
ratpack {
    HikariDataSource ds
    serverConfig {
        ServerConfigBuilder config ->
            config
                    .baseDir(BaseDir.find())
                    .props("config.properties")
                    .props("datasource.properties")
                    .env()
                    .sysProps()
                    .require("/datasource", JdbcCfg) // require part of config
    }

    bindings {
        JdbcCfg jdbcCfg

        module MarkupTemplateModule
        module SessionModule

        serverConfig.requiredConfig.each {
            if (it.object instanceof JdbcCfg  ) {
                jdbcCfg = it.object
            }
        }


        add (HikariModule,  { HikariConfig c ->
            c.setDriverClassName(jdbcCfg.driverClassName)
            c.setConnectionTestQuery(jdbcCfg.connectionTestQuery)
            c.setJdbcUrl(jdbcCfg.jdbcUrl)
            c.setUsername(jdbcCfg.username)
            c.setPassword(jdbcCfg.password)
            c.setMaximumPoolSize(jdbcCfg.maximumPoolSize)
            c.setConnectionTimeout(jdbcCfg.connectionTimeout)
            c.setPoolName(jdbcCfg.poolName)

            ds = new HikariDataSource(c);
        })

    }
...
rus
Reply | Threaded
Open this post in threaded view
|

Re: Setting HikariConfig and getting IllegalStateException

rus
Reply | Threaded
Open this post in threaded view
|

Re: Setting HikariConfig and getting IllegalStateException

PL99
rus, thank you for the response. This example is working fine.