自定义Unwrappers
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
JDBC 知道 java.sql.Wrapper API,它由所有 JDBC 类型实现,以便能够“解包”任何给定类型的原生驱动程序实现。例如
// This may be some proxy from a connection pool
Connection c = getConnection();
// Sometimes, we want the native driver connection instance
OracleConnection oc = c.unwrap(OracleConnection.class);
Array array = oc.createARRAY("ARRAY_TYPE", new Object[] { "a", "b" });
jOOQ 内部偶尔也会进行类似的调用。 为此,它需要解包原生 java.sql.Connection 或 java.sql.PreparedStatement 实例。 遗憾的是,并非所有第三方库都正确实现了 Wrapper API 契约,因此此解包可能无法正常工作。 org.jooq.Unwrapper SPI 旨在允许将自定义实现注入 jOOQ 配置中
// Your jOOQ configuration
Configuration c1 = getConfiguration();
Configuration c2 = c.derive(new Unwrapper() {
@Override
public <T> T unwrap(Wrapper wrapper, Class<T> iface) {
try {
if (wrapper instanceof Connection)
// ...
else if (wrapper instanceof Statement)
// ...
else
wrapper.unwrap(iface);
}
catch (SQLException e) {
// ...
}
}
});
// Work with the derived configuration, where needed
DSL.using(c2).fetch("...");
反馈
您对此页面有任何反馈吗? 我们很乐意听取您的意见!