我正在使用 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/