You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ki...@apache.org on 2020/07/22 13:02:51 UTC
[shardingsphere] branch master updated: AbstractConnectionAdapter
implement Connection#isValid(int) method (#6002)
This is an automated email from the ASF dual-hosted git repository.
kimmking pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new b72bf57 AbstractConnectionAdapter implement Connection#isValid(int) method (#6002)
b72bf57 is described below
commit b72bf57b4d84489561b68c4e2f627e53319e82d2
Author: DanielWei <da...@gmail.com>
AuthorDate: Wed Jul 22 21:02:32 2020 +0800
AbstractConnectionAdapter implement Connection#isValid(int) method (#6002)
---
.../jdbc/adapter/AbstractConnectionAdapter.java | 10 ++++++++++
.../AbstractUnsupportedOperationConnection.java | 7 +------
.../connection/ShardingSphereConnectionTest.java | 20 ++++++++++++++++++++
.../UnsupportedOperationConnectionTest.java | 7 -------
.../circuit/connection/CircuitBreakerConnection.java | 8 +++++++-
5 files changed, 38 insertions(+), 14 deletions(-)
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractConnectionAdapter.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractConnectionAdapter.java
index 8b8bac3..7eae41c 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractConnectionAdapter.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractConnectionAdapter.java
@@ -237,6 +237,16 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
recordMethodInvocation(Connection.class, "setTransactionIsolation", new Class[]{int.class}, new Object[]{level});
forceExecuteTemplate.execute(cachedConnections.values(), connection -> connection.setTransactionIsolation(level));
}
+
+ @Override
+ public final boolean isValid(final int timeout) throws SQLException {
+ for (Connection connection : cachedConnections.values()) {
+ if (!connection.isValid(timeout)) {
+ return false;
+ }
+ }
+ return true;
+ }
// ------- Consist with MySQL driver implementation -------
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java
index 5521090..d3e024e 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java
@@ -151,12 +151,7 @@ public abstract class AbstractUnsupportedOperationConnection extends WrapperAdap
public final Struct createStruct(final String typeName, final Object[] attributes) throws SQLException {
throw new SQLFeatureNotSupportedException("createStruct");
}
-
- @Override
- public final boolean isValid(final int timeout) throws SQLException {
- throw new SQLFeatureNotSupportedException("isValid");
- }
-
+
@Override
public final Properties getClientInfo() throws SQLException {
throw new SQLFeatureNotSupportedException("getClientInfo");
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
index 421e7c1..822518d 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
@@ -42,8 +42,10 @@ import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -128,4 +130,22 @@ public final class ShardingSphereConnectionTest {
connection.rollback();
assertTrue(BASEShardingTransactionManagerFixture.getInvocations().contains(TransactionOperationType.ROLLBACK));
}
+
+ @Test
+ public void assertIsValid() throws SQLException {
+ Connection masterConnection = mock(Connection.class);
+ Connection upSlaveConnection = mock(Connection.class);
+ Connection downSlaveConnection = mock(Connection.class);
+
+ when(masterConnection.isValid(anyInt())).thenReturn(true);
+ when(upSlaveConnection.isValid(anyInt())).thenReturn(true);
+ when(downSlaveConnection.isValid(anyInt())).thenReturn(false);
+
+ connection.getCachedConnections().put("test_master", masterConnection);
+ connection.getCachedConnections().put("test_slave_up", upSlaveConnection);
+ assertTrue(connection.isValid(0));
+
+ connection.getCachedConnections().put("test_slave_down", downSlaveConnection);
+ assertFalse(connection.isValid(0));
+ }
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java
index 08fb6b4..87e6731 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java
@@ -209,13 +209,6 @@ public final class UnsupportedOperationConnectionTest extends AbstractShardingSp
}
@Test(expected = SQLFeatureNotSupportedException.class)
- public void assertIsValid() throws SQLException {
- for (ShardingSphereConnection each : shardingSphereConnections) {
- each.isValid(0);
- }
- }
-
- @Test(expected = SQLFeatureNotSupportedException.class)
public void assertGetClientInfo() throws SQLException {
for (ShardingSphereConnection each : shardingSphereConnections) {
each.getClientInfo();
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/circuit/connection/CircuitBreakerConnection.java b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/circuit/connection/CircuitBreakerConnection.java
index 7103d9a..a9b776b 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/circuit/connection/CircuitBreakerConnection.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/circuit/connection/CircuitBreakerConnection.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.driver.orchestration.internal.circuit.statement
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
+import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
@@ -127,7 +128,12 @@ public final class CircuitBreakerConnection extends AbstractUnsupportedOperation
public PreparedStatement prepareStatement(final String sql, final String[] columnNames) {
return new CircuitBreakerPreparedStatement();
}
-
+
+ @Override
+ public boolean isValid(final int timeout) throws SQLException {
+ return true;
+ }
+
@Override
public Statement createStatement() {
return new CircuitBreakerStatement();