Skip to content

Commit

Permalink
Mock get/set networkTimeout of java.sql.Connection in JDBC Driver t…
Browse files Browse the repository at this point in the history
…o prevent HikariCP's Warning
  • Loading branch information
linghengqian committed Sep 27, 2024
1 parent 513a96f commit 678e4ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.sql.Statement;
import java.util.Collection;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;

/**
* ShardingSphere connection.
Expand Down Expand Up @@ -264,6 +265,30 @@ public void setReadOnly(final boolean readOnly) throws SQLException {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ public final void setTypeMap(final Map<String, Class<?>> 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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 678e4ac

Please sign in to comment.