Madbreaks
Madbreaks

Reputation: 19549

"SQLException: Unwrap error" with JDBI and PGJDBC-NG Postgres Driver

I'm using JDBI to connect to my Postgres db. This has been working fine. Today I decided to try replacing the native Postgres driver with PGJDBC-NG driver. Everything is fine until I try my first simple query:

jdbi.useExtension(FooDao.class, dao -> {
    dao.insert(e);
});

This unfortunately results in:

org.jdbi.v3.core.ConnectionException: java.sql.SQLException: Unwrap error

Debugging into the app I find the exception occurs in the customizeHandle method of JDBI's PostgresPlugin class:

@Override
public Handle customizeHandle(Handle handle) {
    PGConnection pgConnection = Unchecked.supplier(() -> handle.getConnection().unwrap(PGConnection.class)).get();
    return handle.configure(PostgresTypes.class, pt -> pt.addTypesToConnection(pgConnection));
}

The exception is thrown on the first line, in the unwrap method. The problem it seems is that with the new driver, getConnection returns an instance of PGDirectConnection, which is not assignable from PGConnection, as the unwrap method call specifies.

Is there a work-around for this? I'm thinking I could just extend PostgresPlugin and override the implementation of customizeHandle to unwrap using PGDirectConnection but I'd prefer not to if possible.

Edit: Ugh, can't override customizeHandle because PostgresTypes.addTypesToConnection isn't public.

Upvotes: 1

Views: 557

Answers (1)

kdubb
kdubb

Reputation: 483

The PostgresPlugin JDBI is querying for PGConnection directly; which is a class only available in the standard driver. So until it's altered to work with NG, it won't.

Although, according to the docs...

The plugin configures mappings for the Java 8 java.time types like Instant or Duration, InetAddress, UUID, typed enums, and hstore.

but none of this is necessary because PGJDBC-NG supports all of these types natively!

Solution

Don't use the PostgresPlugin

Upvotes: 1

Related Questions