HikariCP用Javassist生成的那个代理有啥用?

发布于 2021-12-04 02:40:49 字数 785 浏览 759 评论 3

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兮颜 2021-12-08 13:32:22

作者如是说:

 

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

 

 

平定天下 2021-12-08 06:16:45

顶起来!

终遇你 2021-12-06 17:56:29

作者最新回复:

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:

{
  try {
    return delegate.method($);
  } catch (SQLException e) {
    throw checkException(e);
  }
}

... 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文