WrapperAdapterのシャーディング-JDBCの話

シーケンス

本稿では、WrapperAdapterのシャーディング-JDBCを見ます

ラッパー

JDK-12.jdk /コンテンツ/ホーム/ libに/ src.zip!/java.sql/java/sql/Wrapper.java

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;

}
复制代码
  • ラッパー・インターフェースはisWrapperFor方法、アンラップ定義します

WrapperAdapter

インキュベーター-shardingsphere-4.0.0-RC1 / shardingjdbc / shardingjdbcコア/ srcに/メイン/ javaの/組織/ apacheの/ shardingsphere / shardingjdbc / JDBC /アダプタ/ WrapperAdapter.java

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宣言JdbcMethodInvocationのセットを定義java.sql.Wrapperインターフェースを達成する; recordMethodInvocation方法はjdbcMethodInvocationsにJdbcMethodInvocationを追加し、replayMethodsInvocation方法は、順番に実行JdbcMethodInvocationのメソッドを呼び出します

JdbcMethodInvocation

インキュベーター-shardingsphere-4.0.0-RC1 / shardingjdbc / shardingjdbcコア/ srcに/メイン/ javaの/組織/ apacheの/ shardingsphere / shardingjdbc / JDBC /アダプタ/起動/ JdbcMethodInvocation.java

@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宣言JdbcMethodInvocationのセットを定義java.sql.Wrapperインターフェースを達成する; recordMethodInvocation方法はjdbcMethodInvocationsにJdbcMethodInvocationを追加し、replayMethodsInvocation方法は、順番に実行JdbcMethodInvocationのメソッドを呼び出します

ドキュメント

おすすめ

転載: juejin.im/post/5d4d8c29e51d45620c1c5384