提交 7a617c44 编写于 作者: G Gao Hongtao 提交者: gaohongtao

fixed #194 Part of the jdbc objects can not be released

上级 2c3fae04
......@@ -19,6 +19,8 @@ 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;
......@@ -69,16 +71,22 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
@Override
public final void rollback() throws SQLException {
for (Connection each : getConnections()) {
each.rollback();
}
SQLUtil.safeInvoke(getConnections(), new ThrowableSQLExceptionMethod<Connection>() {
@Override
public void apply(final Connection object) throws SQLException {
object.rollback();
}
});
}
@Override
public void close() throws SQLException {
for (Connection each : getConnections()) {
each.close();
}
SQLUtil.safeInvoke(getConnections(), new ThrowableSQLExceptionMethod<Connection>() {
@Override
public void apply(final Connection object) throws SQLException {
object.close();
}
});
closed = true;
MetricsContext.clear();
}
......
......@@ -18,6 +18,8 @@
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.AccessLevel;
import lombok.Getter;
......@@ -63,9 +65,12 @@ public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperat
@Override
public final void close() throws SQLException {
for (ResultSet each : resultSets) {
each.close();
}
SQLUtil.safeInvoke(resultSets, new ThrowableSQLExceptionMethod<ResultSet>() {
@Override
public void apply(final ResultSet object) throws SQLException {
object.close();
}
});
closed = true;
}
......
......@@ -18,6 +18,8 @@
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.ResultSet;
......@@ -45,10 +47,14 @@ public abstract class AbstractStatementAdapter extends AbstractUnsupportedOperat
protected abstract void clearRouteStatements();
@Override
@SuppressWarnings("unchecked")
public final void close() throws SQLException {
for (Statement each : getRoutedStatements()) {
each.close();
}
SQLUtil.safeInvoke(getRoutedStatements(), new ThrowableSQLExceptionMethod() {
@Override
public void apply(final Object object) throws SQLException {
((Statement) object).close();
}
});
closed = true;
clearRouteStatements();
}
......
......@@ -21,6 +21,9 @@ import com.google.common.base.CharMatcher;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import java.sql.SQLException;
import java.util.Collection;
/**
* SQL工具类.
*
......@@ -38,4 +41,32 @@ 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;
}
......@@ -34,10 +34,10 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
public class ShardingConnectionTest {
private static final DataSource MASTER_DATA_SOURCE = new TestDataSource("test_ds_master");
......@@ -106,9 +106,20 @@ public class ShardingConnectionTest {
}
@Test
public void releaseBrokenConnectionTest() throws Exception {
public void releaseBrokenConnection() throws Exception {
Connection conn = connection.getConnection(DS_NAME, SQLStatementType.UPDATE);
connection.releaseBrokenConnection(conn);
assertNotSame(conn, connection.getConnection(DS_NAME, SQLStatementType.UPDATE));
}
@Test
public void closeExceptionConnection() throws SQLException {
connection.getConnection(DS_NAME, SQLStatementType.SELECT);
connection.getConnection(DS_NAME, SQLStatementType.UPDATE);
try {
connection.close();
} catch (final SQLException exp) {
assertNotNull(exp.getNextException());
}
}
}
......@@ -8,6 +8,10 @@ weight = 1
## 1.4.1-SNAPSHOT
### 缺陷修正
1. [ISSUE #194](https://github.com/dangdangdotcom/sharding-jdbc/issues/194) jdbc接口中资源释放错误
## 1.4.0
### 功能提升
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册