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

Lucare 发布于 2018/08/29 20:24
阅读 292
收藏 0
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才是真正意义上的代理,那我为啥不直接使用其父类啊?

 

不懂他搞这一层有什么意义。

加载中
0
Lucare
Lucare

作者最新回复:

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.

0
Lucare
Lucare

顶起来!

0
Lucare
Lucare

作者如是说:

 

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

 

 

返回顶部
顶部