Skip to content

Commit 1ed6964

Browse files
hanahmilygaohongtao
authored and
gaohongtao
committed
fix #112 bed get new connection when old one is broken
1 parent 1a20287 commit 1ed6964

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

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

+22-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.dangdang.ddframe.rdb.sharding.parser.result.router.SQLStatementType;
2525
import com.google.common.base.Joiner;
2626
import com.google.common.base.Optional;
27+
import com.google.common.base.Preconditions;
2728
import lombok.AccessLevel;
2829
import lombok.Getter;
2930
import lombok.RequiredArgsConstructor;
@@ -65,6 +66,26 @@ public Connection getConnection(final String dataSourceName, final SQLStatementT
6566
return result;
6667
}
6768

69+
/**
70+
* 释放缓存中已经中断的数据库连接.
71+
*
72+
* @param brokenConnection 已经中断的数据库连接
73+
*/
74+
public void releaseBrokenConnection(final Connection brokenConnection) {
75+
Preconditions.checkNotNull(brokenConnection);
76+
closeConnection(brokenConnection);
77+
connectionMap.values().remove(brokenConnection);
78+
}
79+
80+
private void closeConnection(final Connection connection) {
81+
if (null != connection) {
82+
try {
83+
connection.close();
84+
} catch (final SQLException ignored) {
85+
}
86+
}
87+
}
88+
6889
@Override
6990
public DatabaseMetaData getMetaData() throws SQLException {
7091
return getConnection(shardingContext.getShardingRule().getDataSourceRule().getDataSourceNames().iterator().next(), SQLStatementType.SELECT).getMetaData();
@@ -93,13 +114,7 @@ private String getRealDataSourceName(final String dataSourceName, final SQLState
93114
if (!MasterSlaveDataSource.isDML(sqlStatementType)) {
94115
return slaveDataSourceName;
95116
}
96-
Connection slaveConnection = connectionMap.remove(slaveDataSourceName);
97-
if (null != slaveConnection) {
98-
try {
99-
slaveConnection.close();
100-
} catch (final SQLException ignored) {
101-
}
102-
}
117+
closeConnection(connectionMap.remove(slaveDataSourceName));
103118
return getMasterDataSourceName(dataSourceName);
104119
}
105120

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

+7
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,11 @@ public void getConnectionMixed() throws Exception {
103103
assertSame(masterConnection, connection.getConnection(DS_NAME, SQLStatementType.SELECT));
104104
assertSame(masterConnection, connection.getConnection(DS_NAME, SQLStatementType.UPDATE));
105105
}
106+
107+
@Test
108+
public void releaseBrokenConnectionTest() throws Exception {
109+
Connection conn = connection.getConnection(DS_NAME, SQLStatementType.UPDATE);
110+
connection.releaseBrokenConnection(conn);
111+
assertNotSame(conn, connection.getConnection(DS_NAME, SQLStatementType.UPDATE));
112+
}
106113
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ weight = 1
1919

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

2324
## 1.3.2
2425

sharding-jdbc-transaction-parent/sharding-jdbc-transaction/src/main/java/com/dangdang/ddframe/rdb/transaction/soft/bed/sync/BestEffortsDeliveryListener.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ public void listen(final DMLExecutionEvent event) {
7272
Connection conn = null;
7373
PreparedStatement preparedStatement = null;
7474
try {
75-
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource(), SQLStatementType.SELECT);
75+
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource(), SQLStatementType.UPDATE);
7676
if (!isValidConnection(conn)) {
77-
conn = bedSoftTransaction.getConnection();
77+
bedSoftTransaction.getConnection().releaseBrokenConnection(conn);
78+
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource(), SQLStatementType.UPDATE);
7879
isNewConnection = true;
7980
}
8081
preparedStatement = conn.prepareStatement(event.getSql());

0 commit comments

Comments
 (0)