提交 5b703313 编写于 作者: T terrymanu

remove ThrowableSQLExceptionMethod from SQLUtil

上级 abc5011e
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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;
}
}
......@@ -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;
}
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.util;
import java.sql.SQLException;
/**
* 可抛出{@linkplain java.sql.SQLException}的方法.
*
* @author gaohongtao.
*/
public interface ThrowableSQLExceptionMethod<T> {
/**
* 调用对象中的方法可能抛出{@linkplain java.sql.SQLException}.
*
* @param object 调用方法的对象
* @throws SQLException 访问数据库错误可以抛出该异常
*/
void apply(T object) throws SQLException;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册