How do I properly use jdbi.open?

I’m trying to spin up a new service and would love to use the JDBI SqlObjects interface.

I’ve written up a little program in Java that successfully creates an instance of org.skife.jdbi.v2.DBI and is able to query the database, however, when I try to get an instance of my Dao class I get a bunch of errors:

What I’m doing:

import com.sevenrooms.crm.data.db.HikariConfigProvider;
import com.sevenrooms.crm.data.db.HikariDatasourceProvider;
import com.zaxxer.hikari.HikariConfig;
import org.skife.jdbi.v2.DBI;

import java.sql.ResultSet;

public class Scratch {
  public static void main(String[] args) throws Exception {

    DBI dbi = new DBI(dataSourceProvider.getDatasource());

    // Verify that we have a real connection and are able to query the database
    ResultSet rs = dbi.open()
        .getConnection()
        .prepareCall("SELECT object_id from object_states limit 1")
        .executeQuery();

    while(rs.next()) {
      System.out.println("got id:" + rs.getInt(1)); // <-- THIS WORKS!
    }

    MyDao myDao = dbi.open(MyDao.class); // <-- This Throws
  }
}

I’ve been scouring the docs and trying to figure out what is broken here but I’m at a complete loss.

Here’s the Dao class:

public interface MyDao {
  @SingleValueResult
  @SqlQuery("SELECT object_id FROM object_states LIMIT 1")
  int getObjectId();
}

From my pom.xml


        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>5.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.jdbi</groupId>
            <artifactId>jdbi</artifactId>
            <version>2.78</version>
        </dependency>

I’m running this in Java 20.

This is what i’m seeing in my console, any idea whats broken here?

got id: 1
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.skife.jdbi.v2.sqlobject.SqlObject.buildSqlObject(SqlObject.java:71)
    at org.skife.jdbi.v2.sqlobject.SqlObjectBuilder.open(SqlObjectBuilder.java:50)
    at org.skife.jdbi.v2.DBI.open(DBI.java:401)
    at Scratch.main(Scratch.java:27)
Caused by: java.lang.IllegalStateException: Unable to load cache item
    at org.skife.jdbi.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:79)
    at org.skife.jdbi.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
    at org.skife.jdbi.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:105)
    at org.skife.jdbi.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:278)
    at org.skife.jdbi.cglib.core.KeyFactory$Generator.create(KeyFactory.java:221)
    at org.skife.jdbi.cglib.core.KeyFactory.create(KeyFactory.java:174)
    at org.skife.jdbi.cglib.core.KeyFactory.create(KeyFactory.java:153)
    at org.skife.jdbi.cglib.proxy.Enhancer.<clinit>(Enhancer.java:73)
    ... 4 more
Caused by: java.lang.ExceptionInInitializerError
    at org.skife.jdbi.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:243)
    at org.skife.jdbi.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at org.skife.jdbi.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:312)
    at org.skife.jdbi.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:85)
    at org.skife.jdbi.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:83)
    at org.skife.jdbi.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
    at org.skife.jdbi.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
    ... 11 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @1188e820
    at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:387)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:363)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:311)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:201)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:195)
    at org.skife.jdbi.cglib.core.ReflectUtils$1.run(ReflectUtils.java:54)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:319)
    at org.skife.jdbi.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:44)
    ... 19 more

  • Search for this: module java.base does not "opens java.lang" to unnamed module @1188e820. I’d also consider using version 3 of that library.

    – 




Migrating to JDBI3 fixed the issue.

Leave a Comment