"SQLException: Unwrap error" 使用 JDBI 和 PGJDBC-NG Postgres 驱动程序

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

我正在使用 JDBI 连接到我的 Postgres 数据库。这一直很好用。今天我决定尝试用 PGJDBC-NG 驱动程序替换本机 Postgres 驱动程序。一切都很好,直到我尝试我的第一个简单查询:

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

不幸的是,这导致:

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

调试应用程序我发现异常发生在 JDBI PostgresPlugin class:

customizeHandle 方法中
@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));
}

第一行抛出异常,在unwrap方法中。问题似乎是,对于新的驱动程序,getConnection returns PGDirectConnection 的实例不能从 PGConnection 分配,正如 unwrap 方法调用指定的那样。

有解决办法吗?我想我可以扩展 PostgresPlugin 并覆盖 customizeHandle 的实现以使用 PGDirectConnection 解包,但如果可能的话我不想这样做。

编辑:呃,无法覆盖 customizeHandle 因为 PostgresTypes.addTypesToConnection 不是 public.

PostgresPlugin JDBI 正在直接查询 PGConnection;这是仅在标准驱动程序中可用的 class。因此,在将其更改为与 NG 一起使用之前,它不会。

尽管根据文档...

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

但是 none 这是必要的,因为 PGJDBC-NG 原生支持所有这些类型!

解决方案

不要使用 PostgresPlugin