From 4121c6dc0063e2b24234aed1a563e7bb8d149501 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 20 Jun 2024 13:10:55 +0800 Subject: [PATCH] fix(jdbc-sink): set network timeout (#17244) (#17322) Co-authored-by: StrikeW --- .../java/com/risingwave/connector/JdbcUtils.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/java/connector-node/risingwave-sink-jdbc/src/main/java/com/risingwave/connector/JdbcUtils.java b/java/connector-node/risingwave-sink-jdbc/src/main/java/com/risingwave/connector/JdbcUtils.java index 7d28e4553fe3..2360ac96585d 100644 --- a/java/connector-node/risingwave-sink-jdbc/src/main/java/com/risingwave/connector/JdbcUtils.java +++ b/java/connector-node/risingwave-sink-jdbc/src/main/java/com/risingwave/connector/JdbcUtils.java @@ -25,6 +25,9 @@ public abstract class JdbcUtils { + static final int CONNECTION_TIMEOUT = 30; + static final int SOCKET_TIMEOUT = 300; + public static Optional getDialectFactory(String jdbcUrl) { if (jdbcUrl.startsWith("jdbc:mysql")) { return Optional.of(new MySqlDialectFactory()); @@ -43,6 +46,16 @@ public static Connection getConnection(String jdbcUrl) throws SQLException { // https://jdbc.postgresql.org/documentation/use/ // https://dev.mysql.com/doc/connectors/en/connector-j-connp-props-networking.html#cj-conn-prop_tcpKeepAlive props.setProperty("tcpKeepAlive", "true"); + + // default timeout in seconds + boolean isPg = jdbcUrl.startsWith("jdbc:postgresql"); + + // postgres use seconds and mysql use milliseconds + int connectTimeout = isPg ? CONNECTION_TIMEOUT : CONNECTION_TIMEOUT * 1000; + int socketTimeout = isPg ? SOCKET_TIMEOUT : SOCKET_TIMEOUT * 1000; + props.setProperty("connectTimeout", String.valueOf(connectTimeout)); + props.setProperty("socketTimeout", String.valueOf(socketTimeout)); + var conn = DriverManager.getConnection(jdbcUrl, props); // disable auto commit can improve performance conn.setAutoCommit(false);