java - "SQLException: Unwrap error"带有 JDBI 和 PGJDBC-NG Postgres 驱动程序

标签 java postgresql jdbc jdbi pgjdbc-ng

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

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

不幸的是,这会导致:

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

在调试应用程序时,我发现异常发生在 JDBI 的 PostgresPlugin 类的 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 返回 PGDirectConnection 的实例。 ,它不能从 PGConnection 分配,正如 unwrap 方法调用指定的那样。

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

编辑:呃,不能覆盖 customizeHandle 因为 PostgresTypes.addTypesToConnection 不是公开的。

最佳答案

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

尽管根据文档...

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

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

解决方案

不要使用PostgresPlugin

关于java - "SQLException: Unwrap error"带有 JDBI 和 PGJDBC-NG Postgres 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56588224/

相关文章:

sql - GROUP BY + CASE 语句

java - DB2 对 JDBC 查询中多个 JOIN 响应缓慢

sql - PostgreSQL - 错误 : relation "table_name" does not exist. 如何在没有架构名称的情况下进行查询?

java - 在 Java 中解析日期的通用方法

java - 红黑树可比

java - RequestMappingHandlerMapping registerMapping 未与 RequestBody/HttpEntity 绑定(bind)

postgresql - postgresql中如何按时间查询

mysql - InnoDB/MySQL - 新事务在 SELECT 上使用旧数据而不是返回更新后的数据

java - 无法使用 sqljdbc4.jar 连接到 MSSQL Server 2012

javascript - 如何更改 Android 中的警报按钮字体