From f00702e6cbf60eb66ebf5c6c6f91152e874609db Mon Sep 17 00:00:00 2001 From: Ling Hengqian Date: Fri, 27 Sep 2024 14:39:57 +0800 Subject: [PATCH] Mock get/set networkTimeout of `java.sql.Connection` in JDBC Driver to prevent HikariCP's Warning (#33024) --- .../connection/ShardingSphereConnection.java | 25 +++++++++++++++++++ ...bstractUnsupportedOperationConnection.java | 4 +-- .../UnsupportedOperationConnectionTest.java | 5 ++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java index 3a6327f06c66c..4b963af32b73d 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java @@ -42,6 +42,7 @@ import java.sql.Statement; import java.util.Collection; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.Executor; /** * ShardingSphere connection. @@ -264,6 +265,30 @@ public void setReadOnly(final boolean readOnly) throws SQLException { databaseConnectionManager.setReadOnly(readOnly); } + /** + * This is just to avoid the Warning in brettwooldridge/HikariCP#2196. + * ShardingSphere does not propagate this property to the real JDBC Driver. + * `0` is actually the default value of {@link java.net.Socket#getSoTimeout()}. + */ + @Override + public int getNetworkTimeout() { + return 0; + } + + /** + * This is just to avoid the Warning in brettwooldridge/HikariCP#2196. + * ShardingSphere does not propagate this property to the real JDBC Driver. + * + * @param executor Not used. + * @param milliseconds The time in milliseconds to wait for the database operation to complete. + */ + @Override + public void setNetworkTimeout(final Executor executor, final int milliseconds) throws SQLException { + if (0 > milliseconds) { + throw new SQLException("Network timeout must be a value greater than or equal to 0."); + } + } + @Override public boolean isValid(final int timeout) throws SQLException { return databaseConnectionManager.isValid(timeout); diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java index a439450835f31..33ecfdd7e1b3a 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedOperationConnection.java @@ -75,12 +75,12 @@ public final void setTypeMap(final Map> map) throws SQLExceptio } @Override - public final int getNetworkTimeout() throws SQLException { + public int getNetworkTimeout() throws SQLException { throw new SQLFeatureNotSupportedException("getNetworkTimeout"); } @Override - public final void setNetworkTimeout(final Executor executor, final int milliseconds) throws SQLException { + public void setNetworkTimeout(final Executor executor, final int milliseconds) throws SQLException { throw new SQLFeatureNotSupportedException("setNetworkTimeout"); } diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java index 91d0144f685c5..3441a0a8610df 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java @@ -31,6 +31,7 @@ import java.util.Collections; import java.util.Properties; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; @@ -78,12 +79,12 @@ void assertSetTypeMap() { @Test void assertGetNetworkTimeout() { - assertThrows(SQLFeatureNotSupportedException.class, shardingSphereConnection::getNetworkTimeout); + assertDoesNotThrow(shardingSphereConnection::getNetworkTimeout); } @Test void assertSetNetworkTimeout() { - assertThrows(SQLFeatureNotSupportedException.class, () -> shardingSphereConnection.setNetworkTimeout(null, 0)); + assertDoesNotThrow(() -> shardingSphereConnection.setNetworkTimeout(null, 0)); } @Test