Skip to content

Commit

Permalink
fix #122 bed get new connection when old one is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
hanahmily authored and gaohongtao committed Oct 20, 2016
1 parent 1a20287 commit ec657f6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.dangdang.ddframe.rdb.sharding.parser.result.router.SQLStatementType;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -65,6 +66,26 @@ public Connection getConnection(final String dataSourceName, final SQLStatementT
return result;
}

/**
* 释放缓存中已经中断的数据库连接.
*
* @param brokenConnection 已经中断的数据库连接
*/
public void releaseBrokenConnection(final Connection brokenConnection) {
Preconditions.checkNotNull(brokenConnection);
closeConnection(brokenConnection);
connectionMap.values().remove(brokenConnection);
}

private void closeConnection(final Connection connection) {
if (null != connection) {
try {
connection.close();
} catch (final SQLException ignored) {
}
}
}

@Override
public DatabaseMetaData getMetaData() throws SQLException {
return getConnection(shardingContext.getShardingRule().getDataSourceRule().getDataSourceNames().iterator().next(), SQLStatementType.SELECT).getMetaData();
Expand Down Expand Up @@ -93,13 +114,7 @@ private String getRealDataSourceName(final String dataSourceName, final SQLState
if (!MasterSlaveDataSource.isDML(sqlStatementType)) {
return slaveDataSourceName;
}
Connection slaveConnection = connectionMap.remove(slaveDataSourceName);
if (null != slaveConnection) {
try {
slaveConnection.close();
} catch (final SQLException ignored) {
}
}
closeConnection(connectionMap.remove(slaveDataSourceName));
return getMasterDataSourceName(dataSourceName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,11 @@ public void getConnectionMixed() throws Exception {
assertSame(masterConnection, connection.getConnection(DS_NAME, SQLStatementType.SELECT));
assertSame(masterConnection, connection.getConnection(DS_NAME, SQLStatementType.UPDATE));
}

@Test
public void releaseBrokenConnectionTest() throws Exception {
Connection conn = connection.getConnection(DS_NAME, SQLStatementType.UPDATE);
connection.releaseBrokenConnection(conn);
assertNotSame(conn, connection.getConnection(DS_NAME, SQLStatementType.UPDATE));
}
}
1 change: 1 addition & 0 deletions sharding-jdbc-doc/content/post/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ weight = 1

1. [ISSUE #149](https://github.com/dangdangdotcom/sharding-jdbc/issues/149) INSERT IGNORE INTO时如果数据重了忽略时返回的成-1了,应该返回0
1. [ISSUE #118](https://github.com/dangdangdotcom/sharding-jdbc/issues/118) 同一个线程内先执行DQL后执行DML,DML操作在从库上执行
1. [ISSUE #122](https://github.com/dangdangdotcom/sharding-jdbc/issues/122) bed的fail重试问题

## 1.3.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ public void listen(final DMLExecutionEvent event) {
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource(), SQLStatementType.SELECT);
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource(), SQLStatementType.UPDATE);
if (!isValidConnection(conn)) {
conn = bedSoftTransaction.getConnection();
bedSoftTransaction.getConnection().releaseBrokenConnection(conn);
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource(), SQLStatementType.UPDATE);
isNewConnection = true;
}
preparedStatement = conn.prepareStatement(event.getSql());
Expand Down

0 comments on commit ec657f6

Please sign in to comment.