From b6c4a0be0f919f1268966b70060df217461b03b7 Mon Sep 17 00:00:00 2001 From: zipg Date: Fri, 14 Aug 2026 14:40:30 +0800 Subject: [PATCH] fix(dameng): bound connection initialization timeout --- .../com/dbx/agent/dameng/DamengAgent.java | 75 ++++++++++++++++++- .../com/dbx/agent/dameng/DamengAgentTest.java | 56 ++++++++++---- 2 files changed, 116 insertions(+), 15 deletions(-) diff --git a/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java b/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java index e0b3582903..d06e8008b0 100644 --- a/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java +++ b/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java @@ -20,6 +20,7 @@ import com.dbx.agent.TableInfo; import com.dbx.agent.TriggerInfo; import java.io.PrintStream; import java.io.Reader; +import java.net.SocketTimeoutException; import java.sql.Blob; import java.sql.Clob; import java.sql.Connection; @@ -40,7 +41,9 @@ import java.sql.Types; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; +import java.util.IdentityHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; @@ -50,6 +53,7 @@ import java.util.Set; public final class DamengAgent extends AbstractJdbcAgent { private static final String AGENT_VERSION = "9999.06.04.1-fix-default"; private static final int DBMS_OUTPUT_ENABLE_TIMEOUT_SECS = 5; + private static final int DBMS_OUTPUT_ENABLE_NETWORK_TIMEOUT_MILLIS = 5_000; private static final String DAMENG_CLASSIFIED_OBJECT_TYPE_SQL = "CASE WHEN o.OBJECT_TYPE = 'MATERIALIZED VIEW' OR (o.OBJECT_TYPE = 'VIEW' AND mv.MVIEW_NAME IS NOT NULL) " + "THEN 'MATERIALIZED_VIEW' ELSE o.OBJECT_TYPE END"; @@ -81,6 +85,7 @@ public final class DamengAgent extends AbstractJdbcAgent { ) mv ON mv.OWNER = o.OWNER AND mv.MVIEW_NAME = o.OBJECT_NAME """.stripIndent().trim(); private String connectedUsername; + private volatile boolean dbmsOutputInitializationSupported = true; @Override protected String driverClass() { @@ -111,14 +116,80 @@ public final class DamengAgent extends AbstractJdbcAgent { @Override protected void afterPhysicalConnect(ConnectParams params, Connection connection) throws SQLException { + if (!dbmsOutputInitializationSupported) { + return; + } + + Integer originalNetworkTimeout = applyDbmsOutputNetworkTimeout(connection); + SQLException setupError = null; try (Statement statement = connection.createStatement()) { statement.setQueryTimeout(DBMS_OUTPUT_ENABLE_TIMEOUT_SECS); statement.execute("BEGIN DBMS_OUTPUT.ENABLE(1000000); END;"); } catch (SQLException error) { - if (!isIgnorableDbmsOutputError(error)) { - throw error; + setupError = error; + } + + boolean setupTimedOut = setupError != null && isTimeoutError(setupError); + if (originalNetworkTimeout != null && !setupTimedOut) { + try { + connection.setNetworkTimeout(Runnable::run, originalNetworkTimeout); + } catch (SQLException restoreError) { + dbmsOutputInitializationSupported = false; + if (setupError != null) { + restoreError.addSuppressed(setupError); + } + throw restoreError; } } + + if (setupError == null) { + return; + } + if (setupTimedOut) { + // Force Hikari to discard this connection. Its retry uses the same agent + // instance and skips optional DBMS_OUTPUT initialization. + dbmsOutputInitializationSupported = false; + throw setupError; + } + if (isIgnorableDbmsOutputError(setupError)) { + dbmsOutputInitializationSupported = false; + return; + } + throw setupError; + } + + private static Integer applyDbmsOutputNetworkTimeout(Connection connection) throws SQLException { + try { + int originalNetworkTimeout = connection.getNetworkTimeout(); + connection.setNetworkTimeout(Runnable::run, DBMS_OUTPUT_ENABLE_NETWORK_TIMEOUT_MILLIS); + return originalNetworkTimeout; + } catch (SQLFeatureNotSupportedException | AbstractMethodError | UnsupportedOperationException ignored) { + return null; + } + } + + private static boolean isTimeoutError(Throwable error) { + return isTimeoutError(error, Collections.newSetFromMap(new IdentityHashMap<>())); + } + + private static boolean isTimeoutError(Throwable error, Set visited) { + if (error == null || !visited.add(error)) { + return false; + } + if (error instanceof SQLTimeoutException || error instanceof SocketTimeoutException) { + return true; + } + String message = error.getMessage(); + if (message != null) { + String normalized = message.toLowerCase(Locale.ROOT); + if (normalized.contains("timeout") || normalized.contains("timed out") || normalized.contains("超时")) { + return true; + } + } + if (error instanceof SQLException sqlError && isTimeoutError(sqlError.getNextException(), visited)) { + return true; + } + return isTimeoutError(error.getCause(), visited); } private static boolean isIgnorableDbmsOutputError(SQLException error) { diff --git a/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentTest.java b/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentTest.java index 0ff83e0936..09cd4ce930 100644 --- a/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentTest.java +++ b/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentTest.java @@ -15,10 +15,10 @@ import org.junit.jupiter.api.Test; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.net.SocketTimeoutException; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; -import java.sql.SQLTimeoutException; import java.sql.SQLTransientConnectionException; import java.sql.Statement; import java.util.ArrayList; @@ -63,36 +63,49 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { void physicalConnectionsEnableDbmsOutputWithoutChangingUserSql() throws Exception { List executedSql = new ArrayList<>(); List queryTimeouts = new ArrayList<>(); + List networkTimeouts = new ArrayList<>(); DamengAgent agent = new DamengAgent(); - agent.afterPhysicalConnect(null, printMessageConnection(null, executedSql, queryTimeouts)); + agent.afterPhysicalConnect(null, printMessageConnection(null, executedSql, queryTimeouts, networkTimeouts)); assertEquals(List.of(5), queryTimeouts); + assertEquals(List.of(5_000, 0), networkTimeouts); assertEquals(List.of("BEGIN DBMS_OUTPUT.ENABLE(1000000); END;"), executedSql); } @Test void physicalConnectionsIgnoreUnsupportedOrRestrictedDbmsOutput() { - DamengAgent agent = new DamengAgent(); - - assertDoesNotThrow(() -> agent.afterPhysicalConnect( + assertDoesNotThrow(() -> new DamengAgent().afterPhysicalConnect( null, failingDbmsOutputConnection(new SQLFeatureNotSupportedException("unsupported", "0A000")) )); - assertDoesNotThrow(() -> agent.afterPhysicalConnect( + assertDoesNotThrow(() -> new DamengAgent().afterPhysicalConnect( null, failingDbmsOutputConnection(new SQLException("permission denied", "42000")) )); } @Test - void physicalConnectionsIgnoreTimedOutDbmsOutputInitialization() { + void physicalConnectionsDisableDbmsOutputAfterInitializationTimeout() { DamengAgent agent = new DamengAgent(); + SQLException timeout = new SQLException("network communication failed"); + timeout.initCause(new SocketTimeoutException("Read timed out")); + List retrySql = new ArrayList<>(); + List networkTimeouts = new ArrayList<>(); + assertSame(timeout, assertThrows( + SQLException.class, + () -> agent.afterPhysicalConnect( + null, + statementConnection(null, new ArrayList<>(), new ArrayList<>(), networkTimeouts, timeout) + ) + )); + assertEquals(List.of(5_000), networkTimeouts); assertDoesNotThrow(() -> agent.afterPhysicalConnect( null, - failingDbmsOutputConnection(new SQLTimeoutException("DBMS_OUTPUT enable timed out")) + printMessageConnection(null, retrySql) )); + assertTrue(retrySql.isEmpty()); } @Test @@ -382,17 +395,27 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { List executedSql, List queryTimeouts ) { - return statementConnection(printMessage, executedSql, queryTimeouts, null); + return printMessageConnection(printMessage, executedSql, queryTimeouts, new ArrayList<>()); + } + + private static Connection printMessageConnection( + String printMessage, + List executedSql, + List queryTimeouts, + List networkTimeouts + ) { + return statementConnection(printMessage, executedSql, queryTimeouts, networkTimeouts, null); } private static Connection failingDbmsOutputConnection(SQLException failure) { - return statementConnection(null, new ArrayList<>(), new ArrayList<>(), failure); + return statementConnection(null, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), failure); } private static Connection statementConnection( String printMessage, List executedSql, List queryTimeouts, + List networkTimeouts, SQLException executeFailure ) { InvocationHandler statementHandler = (Object unused, Method method, Object[] args) -> { @@ -420,10 +443,17 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { statementHandler ); InvocationHandler connectionHandler = (Object unused, Method method, Object[] args) -> { - if (method.getName().equals("createStatement")) { - return statement; + switch (method.getName()) { + case "createStatement": + return statement; + case "getNetworkTimeout": + return 0; + case "setNetworkTimeout": + networkTimeouts.add((Integer) args[1]); + return null; + default: + return defaultValue(method.getReturnType()); } - return defaultValue(method.getReturnType()); }; return (Connection) Proxy.newProxyInstance( DamengAgentTest.class.getClassLoader(),