Skip to content

Commit 3843a50

Browse files
committed
fix test case
1 parent eb59254 commit 3843a50

File tree

3 files changed

+29
-32
lines changed

3 files changed

+29
-32
lines changed

sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,20 @@ public final void close() throws SQLException {
168168
@Override
169169
public void execute(final Map.Entry<String, Connection> cachedConnectionsEntrySet) throws SQLException {
170170
Connection connection = cachedConnectionsEntrySet.getValue();
171-
ConnectionCloseEvent event = new ConnectionCloseEvent(cachedConnectionsEntrySet.getKey(), connection.getMetaData().getURL());
172-
ShardingEventBusInstance.getInstance().post(event);
173-
try {
174-
connection.close();
175-
event.setExecuteSuccess();
176-
// CHECKSTYLE:OFF
177-
} catch (final Exception ex) {
178-
// CHECKSTYLE:ON
179-
event.setExecuteFailure(ex);
180-
throw ex;
181-
} finally {
171+
if (!connection.isClosed()) {
172+
ConnectionCloseEvent event = new ConnectionCloseEvent(cachedConnectionsEntrySet.getKey(), connection.getMetaData().getURL());
182173
ShardingEventBusInstance.getInstance().post(event);
174+
try {
175+
connection.close();
176+
event.setExecuteSuccess();
177+
// CHECKSTYLE:OFF
178+
} catch (final Exception ex) {
179+
// CHECKSTYLE:ON
180+
event.setExecuteFailure(ex);
181+
throw ex;
182+
} finally {
183+
ShardingEventBusInstance.getInstance().post(event);
184+
}
183185
}
184186
}
185187
});

sharding-opentracing/src/test/java/io/shardingsphere/opentracing/listener/ExecuteEventListenerTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.Test;
3333

3434
import java.sql.Connection;
35+
import java.sql.DatabaseMetaData;
3536
import java.sql.PreparedStatement;
3637
import java.sql.SQLException;
3738
import java.sql.Statement;
@@ -48,6 +49,8 @@
4849

4950
public final class ExecuteEventListenerTest extends BaseEventListenerTest {
5051

52+
private static final String HOST_URL = "jdbc:mysql://127.0.0.1:3306/ds_0";
53+
5154
private ShardingExecuteEngine executeEngine = new ShardingExecuteEngine(5);
5255

5356
private final SQLExecuteTemplate sqlExecuteTemplate = new SQLExecuteTemplate(executeEngine);
@@ -61,6 +64,8 @@ public void tearDown() {
6164
public void assertSingleStatement() throws SQLException {
6265
Statement statement = mock(Statement.class);
6366
when(statement.getConnection()).thenReturn(mock(Connection.class));
67+
when(statement.getConnection().getMetaData()).thenReturn(mock(DatabaseMetaData.class));
68+
when(statement.getConnection().getMetaData().getURL()).thenReturn(HOST_URL);
6469
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
6570
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
6671
SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<Integer>(SQLType.DML, isExceptionThrown, dataMap) {
@@ -80,9 +85,13 @@ public void assertMultiStatement() throws SQLException {
8085
List<StatementExecuteUnit> statementExecuteUnits = new ArrayList<>(2);
8186
Statement stm1 = mock(Statement.class);
8287
when(stm1.getConnection()).thenReturn(mock(Connection.class));
88+
when(stm1.getConnection().getMetaData()).thenReturn(mock(DatabaseMetaData.class));
89+
when(stm1.getConnection().getMetaData().getURL()).thenReturn(HOST_URL);
8390
statementExecuteUnits.add(new StatementExecuteUnit(new RouteUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm1));
8491
Statement stm2 = mock(Statement.class);
8592
when(stm2.getConnection()).thenReturn(mock(Connection.class));
93+
when(stm2.getConnection().getMetaData()).thenReturn(mock(DatabaseMetaData.class));
94+
when(stm2.getConnection().getMetaData().getURL()).thenReturn(HOST_URL);
8695
statementExecuteUnits.add(new StatementExecuteUnit(new RouteUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm2));
8796
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
8897
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
@@ -103,9 +112,13 @@ public void assertBatchPreparedStatement() throws SQLException {
103112
List<List<Object>> parameterSets = Arrays.asList(Arrays.<Object>asList(1, 2), Arrays.<Object>asList(3, 4));
104113
PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
105114
when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
115+
when(preparedStatement1.getConnection().getMetaData()).thenReturn(mock(DatabaseMetaData.class));
116+
when(preparedStatement1.getConnection().getMetaData().getURL()).thenReturn(HOST_URL);
106117
batchPreparedStatementExecuteUnits.add(new BatchPreparedStatementExecuteUnit(new RouteUnit("ds_0", new SQLUnit("insert into ...", parameterSets)), preparedStatement1));
107118
PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
108119
when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
120+
when(preparedStatement2.getConnection().getMetaData()).thenReturn(mock(DatabaseMetaData.class));
121+
when(preparedStatement2.getConnection().getMetaData().getURL()).thenReturn(HOST_URL);
109122
batchPreparedStatementExecuteUnits.add(new BatchPreparedStatementExecuteUnit(new RouteUnit("ds_1", new SQLUnit("insert into ...", parameterSets)), preparedStatement2));
110123
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
111124
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
@@ -124,6 +137,8 @@ protected Integer executeSQL(final SQLExecuteUnit sqlExecuteUnit) {
124137
public void assertSQLException() throws SQLException {
125138
Statement statement = mock(Statement.class);
126139
when(statement.getConnection()).thenReturn(mock(Connection.class));
140+
when(statement.getConnection().getMetaData()).thenReturn(mock(DatabaseMetaData.class));
141+
when(statement.getConnection().getMetaData().getURL()).thenReturn(HOST_URL);
127142
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
128143
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
129144
SQLExecuteCallback<Integer> executeCallback = new SQLExecuteCallback<Integer>(SQLType.DQL, isExceptionThrown, dataMap) {

sharding-proxy/src/main/resources/conf/config_master_slave.yaml

+1-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# We provide two kinds of configurations for example. This one is the configuration of Master slave rule,
55
# If you only want to use master-slave, please refer to this one, and if you want to use Sharding,
6-
# please refer to the config.yaml.
6+
# please refer to the config-sharding_db.yaml.
77
#
88
################## ##################
99

@@ -36,24 +36,4 @@
3636
# masterDataSourceName: ds_0
3737
# slaveDataSourceNames:
3838
# - ds_1
39-
# props:
40-
# # MEMORY_STRICTLY: Proxy holds as many connections as the count of actual tables routed in a database.
41-
# # The benefit of this approach is saving memory for Proxy by Stream ResultSet.
42-
# # CONNECTION_STRICTLY: Proxy will release connections after get the overall rows from the ResultSet.
43-
# # Meanwhile, the cost of the memory will be increased.
44-
# connection.mode: MEMORY_STRICTLY
45-
# acceptor.size: 16 # The default value is available processors count * 2.
46-
# executor.size: 16 # Infinite by default.
47-
# sql.show: false
4839

49-
#orchestration:
50-
# name: orchestration_ds
51-
# type: SHARDING
52-
# overwrite: true
53-
# zookeeper:
54-
# namespace: orchestration
55-
# serverLists: localhost:2181
56-
57-
#proxyAuthority:
58-
# username: root
59-
# password: root

0 commit comments

Comments
 (0)