既然作者已经编写了 ProxyConnection,为什么还要通过 javaassist 生成 HikariProxyConnection?
Why generate HikariProxyConnection by javaassist since author already write ProxyConnection?
根据HikariCP的源码,我发现作者是通过javaassist生成HikariProxyConnection,但是class只是调用了superclass方法
例如HikariProxyConnection的superclass是ProxyConnection:
public class HikariProxyConnection extends ProxyConnection implements AutoCloseable, Connection, Wrapper {
public Statement createStatement() throws SQLException {
try {
return super.createStatement();
} catch (SQLException var2) {
throw this.checkException(var2);
}
}
public PreparedStatement prepareStatement(String var1) throws SQLException {
try {
return super.prepareStatement(var1);
} catch (SQLException var3) {
throw this.checkException(var3);
}
} }
我发现 ProxyConnection 已经做了很多事情,HikariProxyConnection 只为每个方法添加了一个 try catch 块。
谁能给个解释就更好了
@brettwooldridge 回答了 Hikari issue HikariProxyConnection
的目的:
The proxies delegate to the real driver classes. Some proxies, like the one for ResultSet, only intercept a few methods. Without the code generation, the proxy would have to implement all 50+ methods which simply delegate to the wrapped instance.
Code generation, based on reflection, also means that nothing needs to be done when a new JDK version introduces new JDBC methods to existing interfaces.
根据HikariCP的源码,我发现作者是通过javaassist生成HikariProxyConnection,但是class只是调用了superclass方法
例如HikariProxyConnection的superclass是ProxyConnection:
public class HikariProxyConnection extends ProxyConnection implements AutoCloseable, Connection, Wrapper {
public Statement createStatement() throws SQLException {
try {
return super.createStatement();
} catch (SQLException var2) {
throw this.checkException(var2);
}
}
public PreparedStatement prepareStatement(String var1) throws SQLException {
try {
return super.prepareStatement(var1);
} catch (SQLException var3) {
throw this.checkException(var3);
}
} }
我发现 ProxyConnection 已经做了很多事情,HikariProxyConnection 只为每个方法添加了一个 try catch 块。
谁能给个解释就更好了
@brettwooldridge 回答了 Hikari issue HikariProxyConnection
的目的:
The proxies delegate to the real driver classes. Some proxies, like the one for ResultSet, only intercept a few methods. Without the code generation, the proxy would have to implement all 50+ methods which simply delegate to the wrapped instance.
Code generation, based on reflection, also means that nothing needs to be done when a new JDK version introduces new JDBC methods to existing interfaces.