Skip to content

Commit

Permalink
fixed apache#194 Part of the jdbc objects can not be released
Browse files Browse the repository at this point in the history
  • Loading branch information
hanahmily authored and gaohongtao committed Nov 28, 2016
1 parent 2c3fae0 commit 7a617c4
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

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;
Expand Down Expand Up @@ -69,16 +71,22 @@ public final void commit() throws SQLException {

@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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,9 +65,12 @@ private Map<String, Integer> generateColumnLabelIndexMap() throws SQLException {

@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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

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

/**
* SQL工具类.
*
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
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");
Expand Down Expand Up @@ -106,9 +106,20 @@ public void getConnectionMixed() throws Exception {
}

@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());
}
}
}
4 changes: 4 additions & 0 deletions sharding-jdbc-doc/content/post/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

### 功能提升
Expand Down

0 comments on commit 7a617c4

Please sign in to comment.