Skip to content

Commit 8587315

Browse files
hanahmilyxydonne
authored andcommitted
fixed apache#194 Part of the jdbc objects can not be released
1 parent 0e190d5 commit 8587315

File tree

7 files changed

+115
-14
lines changed

7 files changed

+115
-14
lines changed

sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/adapter/AbstractConnectionAdapter.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationConnection;
2121
import com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext;
22+
import com.dangdang.ddframe.rdb.sharding.util.SQLUtil;
23+
import com.dangdang.ddframe.rdb.sharding.util.ThrowableSQLExceptionMethod;
2224

2325
import java.sql.Connection;
2426
import java.sql.ResultSet;
@@ -69,16 +71,22 @@ public final void commit() throws SQLException {
6971

7072
@Override
7173
public final void rollback() throws SQLException {
72-
for (Connection each : getConnections()) {
73-
each.rollback();
74-
}
74+
SQLUtil.safeInvoke(getConnections(), new ThrowableSQLExceptionMethod<Connection>() {
75+
@Override
76+
public void apply(final Connection object) throws SQLException {
77+
object.rollback();
78+
}
79+
});
7580
}
7681

7782
@Override
7883
public void close() throws SQLException {
79-
for (Connection each : getConnections()) {
80-
each.close();
81-
}
84+
SQLUtil.safeInvoke(getConnections(), new ThrowableSQLExceptionMethod<Connection>() {
85+
@Override
86+
public void apply(final Connection object) throws SQLException {
87+
object.close();
88+
}
89+
});
8290
closed = true;
8391
MetricsContext.clear();
8492
}

sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/adapter/AbstractResultSetAdapter.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package com.dangdang.ddframe.rdb.sharding.jdbc.adapter;
1919

2020
import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationResultSet;
21+
import com.dangdang.ddframe.rdb.sharding.util.SQLUtil;
22+
import com.dangdang.ddframe.rdb.sharding.util.ThrowableSQLExceptionMethod;
2123
import com.google.common.base.Preconditions;
2224
import lombok.AccessLevel;
2325
import lombok.Getter;
@@ -63,9 +65,12 @@ private Map<String, Integer> generateColumnLabelIndexMap() throws SQLException {
6365

6466
@Override
6567
public final void close() throws SQLException {
66-
for (ResultSet each : resultSets) {
67-
each.close();
68-
}
68+
SQLUtil.safeInvoke(resultSets, new ThrowableSQLExceptionMethod<ResultSet>() {
69+
@Override
70+
public void apply(final ResultSet object) throws SQLException {
71+
object.close();
72+
}
73+
});
6974
closed = true;
7075
}
7176

sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/adapter/AbstractStatementAdapter.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package com.dangdang.ddframe.rdb.sharding.jdbc.adapter;
1919

2020
import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationStatement;
21+
import com.dangdang.ddframe.rdb.sharding.util.SQLUtil;
22+
import com.dangdang.ddframe.rdb.sharding.util.ThrowableSQLExceptionMethod;
2123
import lombok.RequiredArgsConstructor;
2224

2325
import java.sql.ResultSet;
@@ -45,10 +47,14 @@ public abstract class AbstractStatementAdapter extends AbstractUnsupportedOperat
4547
protected abstract void clearRouteStatements();
4648

4749
@Override
50+
@SuppressWarnings("unchecked")
4851
public final void close() throws SQLException {
49-
for (Statement each : getRoutedStatements()) {
50-
each.close();
51-
}
52+
SQLUtil.safeInvoke(getRoutedStatements(), new ThrowableSQLExceptionMethod() {
53+
@Override
54+
public void apply(final Object object) throws SQLException {
55+
((Statement) object).close();
56+
}
57+
});
5258
closed = true;
5359
clearRouteStatements();
5460
}

sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/util/SQLUtil.java

+31
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import lombok.AccessLevel;
2222
import lombok.AllArgsConstructor;
2323

24+
import java.sql.SQLException;
25+
import java.util.Collection;
26+
2427
/**
2528
* SQL工具类.
2629
*
@@ -38,4 +41,32 @@ public class SQLUtil {
3841
public static String getExactlyValue(final String value) {
3942
return null == value ? null : CharMatcher.anyOf("[]`'\"").removeFrom(value);
4043
}
44+
45+
/**
46+
* 安全的调用一组可能抛出{@linkplain SQLException}的对象中的方法.
47+
* 通过该方法保证后,保证每个对象中的方法均被调用一次
48+
*
49+
* @param throwableSQLExceptionObjects 调用方法可能抛出异常的对象集合
50+
* @param method 方法定义
51+
* @param <T> 对象类型
52+
* @throws SQLException 数据库访问异常会抛出
53+
*/
54+
public static <T> void safeInvoke(final Collection<T> throwableSQLExceptionObjects, final ThrowableSQLExceptionMethod<T> method) throws SQLException {
55+
SQLException current = null;
56+
for (T each : throwableSQLExceptionObjects) {
57+
try {
58+
method.apply(each);
59+
} catch (final SQLException exp) {
60+
if (null == current) {
61+
current = exp;
62+
} else {
63+
current.setNextException(exp);
64+
current = exp;
65+
}
66+
}
67+
}
68+
if (null != current) {
69+
throw current;
70+
}
71+
}
4172
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 1999-2015 dangdang.com.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* </p>
16+
*/
17+
18+
package com.dangdang.ddframe.rdb.sharding.util;
19+
20+
import java.sql.SQLException;
21+
22+
/**
23+
* 可抛出{@linkplain java.sql.SQLException}的方法.
24+
*
25+
* @author gaohongtao.
26+
*/
27+
public interface ThrowableSQLExceptionMethod<T> {
28+
29+
/**
30+
* 调用对象中的方法可能抛出{@linkplain java.sql.SQLException}.
31+
*
32+
* @param object 调用方法的对象
33+
* @throws SQLException 访问数据库错误可以抛出该异常
34+
*/
35+
void apply(T object) throws SQLException;
36+
}

sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingConnectionTest.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
import java.util.HashMap;
3535
import java.util.Map;
3636

37+
import static org.junit.Assert.assertNotNull;
3738
import static org.junit.Assert.assertNotSame;
3839
import static org.junit.Assert.assertSame;
3940

40-
4141
public class ShardingConnectionTest {
4242

4343
private static final DataSource MASTER_DATA_SOURCE = new TestDataSource("test_ds_master");
@@ -106,9 +106,20 @@ public void getConnectionMixed() throws Exception {
106106
}
107107

108108
@Test
109-
public void releaseBrokenConnectionTest() throws Exception {
109+
public void releaseBrokenConnection() throws Exception {
110110
Connection conn = connection.getConnection(DS_NAME, SQLStatementType.UPDATE);
111111
connection.releaseBrokenConnection(conn);
112112
assertNotSame(conn, connection.getConnection(DS_NAME, SQLStatementType.UPDATE));
113113
}
114+
115+
@Test
116+
public void closeExceptionConnection() throws SQLException {
117+
connection.getConnection(DS_NAME, SQLStatementType.SELECT);
118+
connection.getConnection(DS_NAME, SQLStatementType.UPDATE);
119+
try {
120+
connection.close();
121+
} catch (final SQLException exp) {
122+
assertNotNull(exp.getNextException());
123+
}
124+
}
114125
}

sharding-jdbc-doc/content/post/release_notes.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ weight = 1
88

99
## 1.4.1-SNAPSHOT
1010

11+
### 缺陷修正
12+
13+
1. [ISSUE #194](https://github.com/dangdangdotcom/sharding-jdbc/issues/194) jdbc接口中资源释放错误
14+
1115
## 1.4.0
1216

1317
### 功能提升

0 commit comments

Comments
 (0)