前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊sharding-jdbc的WrapperAdapter

聊聊sharding-jdbc的WrapperAdapter

原创
作者头像
code4it
修改2019-08-12 10:40:16
6740
修改2019-08-12 10:40:16
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究一下sharding-jdbc的WrapperAdapter

Wrapper

jdk-12.jdk/Contents/Home/lib/src.zip!/java.sql/java/sql/Wrapper.java

代码语言:javascript
复制
public interface Wrapper {
?
    <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException;
?
    boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException;
?
}
  • Wrapper接口定义了unwrap、isWrapperFor方法

WrapperAdapter

incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/WrapperAdapter.java

代码语言:javascript
复制
public abstract class WrapperAdapter implements Wrapper {
    
    private final Collection<JdbcMethodInvocation> jdbcMethodInvocations = new ArrayList<>();
    
    @SuppressWarnings("unchecked")
    @Override
    public final <T> T unwrap(final Class<T> iface) throws SQLException {
        if (isWrapperFor(iface)) {
            return (T) this;
        }
        throw new SQLException(String.format("[%s] cannot be unwrapped as [%s]", getClass().getName(), iface.getName()));
    }
    
    @Override
    public final boolean isWrapperFor(final Class<?> iface) {
        return iface.isInstance(this);
    }
    
    /**
     * record method invocation.
     * 
     * @param targetClass target class
     * @param methodName method name
     * @param argumentTypes argument types
     * @param arguments arguments
     */
    @SneakyThrows
    public final void recordMethodInvocation(final Class<?> targetClass, final String methodName, final Class<?>[] argumentTypes, final Object[] arguments) {
        jdbcMethodInvocations.add(new JdbcMethodInvocation(targetClass.getMethod(methodName, argumentTypes), arguments));
    }
    
    /**
     * Replay methods invocation.
     * 
     * @param target target object
     */
    public final void replayMethodsInvocation(final Object target) {
        for (JdbcMethodInvocation each : jdbcMethodInvocations) {
            each.invoke(target);
        }
    }
}
  • WrapperAdapter声明实现java.sql.Wrapper接口,它定义了JdbcMethodInvocation集合;recordMethodInvocation方法会往jdbcMethodInvocations添加JdbcMethodInvocation;replayMethodsInvocation方法则会挨个执行JdbcMethodInvocation的invoke方法

JdbcMethodInvocation

incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/invocation/JdbcMethodInvocation.java

代码语言:javascript
复制
@RequiredArgsConstructor
public class JdbcMethodInvocation {
    
    @Getter
    private final Method method;
    
    @Getter
    private final Object[] arguments;
    
    /**
     * Invoke JDBC method.
     * 
     * @param target target object
     */
    @SneakyThrows
    public void invoke(final Object target) {
        method.invoke(target, arguments);
    }
}
  • JdbcMethodInvocation的invoke方法执行的是method.invoke

小结

WrapperAdapter声明实现java.sql.Wrapper接口,它定义了JdbcMethodInvocation集合;recordMethodInvocation方法会往jdbcMethodInvocations添加JdbcMethodInvocation;replayMethodsInvocation方法则会挨个执行JdbcMethodInvocation的invoke方法

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Wrapper
  • WrapperAdapter
  • JdbcMethodInvocation
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com