HikariCP用Javassist生成的那个代理有啥用?
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);
}
}
//......
}
如上所示,是利用JavassistProxyFactory 生成的class反编译后的类,看了一下,就是把所有方法转移到其父类去执行,父类ProxyConnection才是真正意义上的代理,那我为啥不直接使用其父类啊?
不懂他搞这一层有什么意义。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作者如是说:
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.
摘自: https://github.com/brettwooldridge/HikariCP/issues/1198
顶起来!
作者最新回复:
A concrete class is generated from the abstract
ProxyConnection
class. Any methods that are not "overridden" by the abstract class, and that throw SQLException have delegates generated with the following code:... which allows us to inspect the exception to see if it represents a disconnection error.
A side-effect is that, yes, the code ends of being flexible with respect to JDBC API changes -- at least all that we have encountered so far.