Commit 5b703313 authored by terrymanu's avatar terrymanu
Browse files

remove ThrowableSQLExceptionMethod from SQLUtil

parent abc5011e
Loading
Loading
Loading
Loading
+18 −14
Original line number Diff line number Diff line
@@ -19,14 +19,13 @@ package com.dangdang.ddframe.rdb.sharding.jdbc.adapter;

import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationConnection;
import com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext;
import com.dangdang.ddframe.rdb.sharding.util.SQLUtil;
import com.dangdang.ddframe.rdb.sharding.util.ThrowableSQLExceptionMethod;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.Collection;
import java.util.LinkedList;

/**
 * 数据库连接适配类.
@@ -71,25 +70,30 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
    
    @Override
    public final void rollback() throws SQLException {
        SQLUtil.safeInvoke(getConnections(), new ThrowableSQLExceptionMethod<Connection>() {
            @Override
            public void apply(final Connection object) throws SQLException {
                object.rollback();
        Collection<SQLException> exceptions = new LinkedList<>();
        for (Connection each : getConnections()) {
            try {
                each.rollback();
            } catch (final SQLException ex) {
                exceptions.add(ex);
            }
        }
        });
        throwSQLExceptionIfNessesary(exceptions);
    }
    
    @Override
    public void close() throws SQLException {
        SQLUtil.safeInvoke(getConnections(), new ThrowableSQLExceptionMethod<Connection>() {
            
            @Override
            public void apply(final Connection object) throws SQLException {
                object.close();
            }
        });
        closed = true;
        MetricsContext.clear();
        Collection<SQLException> exceptions = new LinkedList<>();
        for (Connection each : getConnections()) {
            try {
                each.close();
            } catch (final SQLException ex) {
                exceptions.add(ex);
            }
        }
        throwSQLExceptionIfNessesary(exceptions);
    }
    
    @Override
+11 −8
Original line number Diff line number Diff line
@@ -18,8 +18,6 @@
package com.dangdang.ddframe.rdb.sharding.jdbc.adapter;

import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationResultSet;
import com.dangdang.ddframe.rdb.sharding.util.SQLUtil;
import com.dangdang.ddframe.rdb.sharding.util.ThrowableSQLExceptionMethod;
import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@@ -28,6 +26,8 @@ import org.apache.commons.collections4.map.CaseInsensitiveMap;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@@ -64,13 +64,16 @@ public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperat
    
    @Override
    public final void close() throws SQLException {
        SQLUtil.safeInvoke(resultSets, new ThrowableSQLExceptionMethod<ResultSet>() {
            @Override
            public void apply(final ResultSet object) throws SQLException {
                object.close();
            }
        });
        closed = true;
        Collection<SQLException> exceptions = new LinkedList<>();
        for (ResultSet each : resultSets) {
            try {
                each.close();
            } catch (final SQLException ex) {
                exceptions.add(ex);
            }
        }
        throwSQLExceptionIfNessesary(exceptions);
    }
    
    @Override
+10 −9
Original line number Diff line number Diff line
@@ -18,14 +18,13 @@
package com.dangdang.ddframe.rdb.sharding.jdbc.adapter;

import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationStatement;
import com.dangdang.ddframe.rdb.sharding.util.SQLUtil;
import com.dangdang.ddframe.rdb.sharding.util.ThrowableSQLExceptionMethod;
import lombok.RequiredArgsConstructor;

import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.Collection;
import java.util.LinkedList;

/**
 * 静态语句对象适配类.
@@ -46,15 +45,17 @@ public abstract class AbstractStatementAdapter extends AbstractUnsupportedOperat
    @Override
    @SuppressWarnings("unchecked")
    public final void close() throws SQLException {
        SQLUtil.safeInvoke(getRoutedStatements(), new ThrowableSQLExceptionMethod() {
            
            @Override
            public void apply(final Object object) throws SQLException {
                ((Statement) object).close();
            }
        });
        closed = true;
        getRoutedStatements().clear();
        Collection<SQLException> exceptions = new LinkedList<>();
        for (Statement each : getRoutedStatements()) {
            try {
                each.close();
            } catch (final SQLException ex) {
                exceptions.add(ex);
            }
        }
        throwSQLExceptionIfNessesary(exceptions);
    }
    
    @Override
+11 −0
Original line number Diff line number Diff line
@@ -74,4 +74,15 @@ public class WrapperAdapter implements Wrapper {
            each.invoke(target);
        }
    }
    
    protected void throwSQLExceptionIfNessesary(final Collection<SQLException> exceptions) throws SQLException {
        if (exceptions.isEmpty()) {
            return;
        }
        SQLException ex = new SQLException();
        for (SQLException each : exceptions) {
            ex.setNextException(each);
        }
        throw ex;
    }
}
+0 −31
Original line number Diff line number Diff line
@@ -21,9 +21,6 @@ import com.google.common.base.CharMatcher;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

import java.sql.SQLException;
import java.util.Collection;

/**
 * SQL工具类.
 * 
@@ -41,32 +38,4 @@ public class SQLUtil {
    public static String getExactlyValue(final String value) {
        return null == value ? null : CharMatcher.anyOf("[]`'\"").removeFrom(value);
    }
    
    /**
     * 安全的调用一组可能抛出{@linkplain SQLException}的对象中的方法.
     * 通过该方法保证后,保证每个对象中的方法均被调用一次
     * 
     * @param throwableSQLExceptionObjects 调用方法可能抛出异常的对象集合
     * @param method 方法定义
     * @param <T> 对象类型
     * @throws SQLException 数据库访问异常会抛出
     */
    public static <T> void safeInvoke(final Collection<T> throwableSQLExceptionObjects, final ThrowableSQLExceptionMethod<T> method) throws SQLException {
        SQLException current = null;
        for (T each : throwableSQLExceptionObjects) {
            try {
                method.apply(each);
            } catch (final SQLException exp) {
                if (null == current) {
                    current = exp;
                } else {
                    current.setNextException(exp);
                    current = exp; 
                }
            }
        }
        if (null != current) {
            throw current;
        }
    }
}
Loading