0

I am trying to use HikariCP with h2 database. Here is my class responsible for sql

@Singleton
@Log
class SqlPersistence {

    def config
    {
        def props = new Properties()
        new File("build/resources/main/db.properties").withInputStream {
            props.load(it)
        }
        log.info "Loaded properties: $props"
        config = new HikariConfig(props)
    }

    @Lazy def ds = new HikariDataSource(config)
    @Lazy def sql = new Sql(ds)

}

But when I try to run it using gradle task I get this error

Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: 'org.h2.jdbcx.JdbcDataSource'
        at com.zaxxer.hikari.util.UtilityElf.createInstance(UtilityElf.java:89)
        at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:293)
        at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:90)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:101)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:71)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
        at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorS
ite.java:102)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:232)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:244)
        at repository.persistence.SqlPersistence.getDs(SqlPersistence.groovy:27)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
        at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty
.java:73)
        at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GtivePogoPropertySite.java:82)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:304)
        at repository.persistence.SqlPersistence.getSql(SqlPersistence.groovy:28)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
        at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty
.java:73)
        at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:8
2)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:304)
        at repository.persistence.SqlPersistence.selectAll(SqlPersistence.groovy:42)
        at repository.persistence.SqlPersistence$selectAll.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:122)
        at repository.BookRepository.findAll(BookRepository.groovy:11)
        at repository.BookRepository$findAll.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:114)
        at service.BookService.findAll(BookService.groovy:13)
        at service.BookService$findAll.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:114)
        at script.run(script.groovy:7)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1207)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
        at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:914)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:897)
        at org.codehaus.groovy.runtime.InvokerHelper.runScript(InvokerHelper.java:407)
        at org.codehaus.groovy.runtime.InvokerHelper$runScript.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
        at script.main(script.groovy)
Caused by: java.lang.ClassNotFoundException: 'org.h2.jdbcx.JdbcDataSource'
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at com.zaxxer.hikari.util.UtilityElf.createInstance(UtilityElf.java:76)
        ... 65 more

My build.gradle looks like this

apply plugin: 'groovy'

repositories {
    jcenter()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.3'
    compile 'com.h2database:h2:1.4.191'
    compile 'com.zaxxer:HikariCP:2.4.5'

    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    testCompile 'junit:junit:4.12'
    testCompile 'cglib:cglib:3.2.1'
}

task runScript(dependsOn: 'compileJava', type: JavaExec) {
    main = 'script'
    classpath = sourceSets.main.runtimeClasspath + sourceSets.main.output
}

Why it's unable to find driver? P.S. there is always some issue with using sql drivers...

lapots
  • 12,553
  • 32
  • 121
  • 242

1 Answers1

0

It looks like your script is Java, but the snippet you posted is Groovy. So I think this is either an issue of how you call the Groovy part so that the H2 lib is not in Groovy classpath, because if I transform your code to Java and put it in script.java it works fine with your build.gradle. But it could also be a Groovy class loading problem. When I tried to access a database from a Gradle script (which essentially is also a Groovy script I had to do the following to get it working correctly:

// load JDBC drivers to the Groovy class loader to overcome java.sql.DriverManager quirks
Sql.classLoader.addURL new File('driver.jar').toURI().toURL()

Sql.withInstance('my:connection:string', 'user', 'pass', 'my.jdbc.DriverClass') {
   it.execute 'select * from foo'
}

Maybe you need something similar.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • My script is `script.groovy` means groovy script. thanks I'll try – lapots Apr 11 '16 at 17:13
  • Did not help. I put it into `script.groovy` did not help - I put it into `sqlpersistence` did not help. – lapots Apr 11 '16 at 18:19
  • Hm, unfortunately I have the same problem now. I'm upgrading Gradle and thus Groovy is upgraded from 1.8.6 to 2.4.7. With 2.4.7 my snippet that worked fine in 1.8.6 does not work anymore. :-( – Vampire Feb 16 '17 at 15:31
  • Ah, got it how it works for me. Maybe helps you too. Updated my answer. – Vampire Feb 16 '17 at 16:10