This is an automated email from the ASF dual-hosted git repository.

jianglongtao 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 f00702e6cbf Mock get/set networkTimeout of `java.sql.Connection` in 
JDBC Driver to prevent HikariCP's Warning (#33024)
f00702e6cbf is described below

commit f00702e6cbf60eb66ebf5c6c6f91152e874609db
Author: Ling Hengqian <[email protected]>
AuthorDate: Fri Sep 27 14:39:57 2024 +0800

    Mock get/set networkTimeout of `java.sql.Connection` in JDBC Driver to 
prevent HikariCP's Warning (#33024)
---
 .../core/connection/ShardingSphereConnection.java  | 25 ++++++++++++++++++++++
 .../AbstractUnsupportedOperationConnection.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 3a6327f06c6..4b963af32b7 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.Savepoint;
 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 final class ShardingSphereConnection extends 
AbstractConnectionAdapter {
         databaseConnectionManager.setReadOnly(readOnly);
     }
     
+    /**
+     * This is just to avoid the Warning in <a 
href="https://github.com/brettwooldridge/HikariCP/issues/2196";>brettwooldridge/HikariCP#2196</a>.
+     * 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 <a 
href="https://github.com/brettwooldridge/HikariCP/issues/2196";>brettwooldridge/HikariCP#2196</a>.
+     * 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 a439450835f..33ecfdd7e1b 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 abstract class 
AbstractUnsupportedOperationConnection extends WrapperAdap
     }
     
     @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 91d0144f685..3441a0a8610 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.sql.SQLFeatureNotSupportedException;
 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 @@ class UnsupportedOperationConnectionTest {
     
     @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

Reply via email to