溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

sharding-jdbc中WrapperAdapter的作用是什么

發(fā)布時間:2021-06-22 15:29:22 來源:億速云 閱讀:210 作者:Leah 欄目:大數(shù)據(jù)

本篇文章為大家展示了sharding-jdbc中WrapperAdapter的作用是什么,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

Wrapper

jdk-12.jdk/Contents/Home/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;

}
  • 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

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聲明實現(xiàn)java.sql.Wrapper接口,它定義了JdbcMethodInvocation集合;recordMethodInvocation方法會往jdbcMethodInvocations添加JdbcMethodInvocation;replayMethodsInvocation方法則會挨個執(zhí)行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

@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方法執(zhí)行的是method.invoke

小結

WrapperAdapter聲明實現(xiàn)java.sql.Wrapper接口,它定義了JdbcMethodInvocation集合;recordMethodInvocation方法會往jdbcMethodInvocations添加JdbcMethodInvocation;replayMethodsInvocation方法則會挨個執(zhí)行JdbcMethodInvocation的invoke方法

上述內容就是sharding-jdbc中WrapperAdapter的作用是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI