From 54e6c855a2e4b4444ab964fa4804687bb272efdb Mon Sep 17 00:00:00 2001 From: Jiri Pechanec Date: Fri, 22 Mar 2024 11:28:18 +0100 Subject: [PATCH] DBZ-7258 Use business methods --- .../storage/jdbc/RetriableConnection.java | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/debezium-storage/debezium-storage-jdbc/src/main/java/io/debezium/storage/jdbc/RetriableConnection.java b/debezium-storage/debezium-storage-jdbc/src/main/java/io/debezium/storage/jdbc/RetriableConnection.java index 8bc6fb9c9..d1fbd3cbf 100644 --- a/debezium-storage/debezium-storage-jdbc/src/main/java/io/debezium/storage/jdbc/RetriableConnection.java +++ b/debezium-storage/debezium-storage-jdbc/src/main/java/io/debezium/storage/jdbc/RetriableConnection.java @@ -52,16 +52,8 @@ public RetriableConnection(String url, String user, String pwd, Duration waitRet createConnection(); } catch (SQLException e) { - LOGGER.error("Unable to create connection. It will be re-attempted during its first use: " + e.getMessage(), e); - if (conn != null) { - try { - conn.close(); - } - catch (Exception ex) { - // ignore - } - conn = null; - } + LOGGER.error("Unable to create connection. It will be re-attempted during its first use: {}", e.getMessage(), e); + close(); } } @@ -72,7 +64,15 @@ private void createConnection() throws SQLException { @Override public void close() throws SQLException { - conn.close(); + if (isConnectionCreated()) { + try { + conn.close(); + } + catch (Exception e) { + LOGGER.warn("Exception while closing connection", e); + } + } + conn = null; } public boolean isConnectionCreated() { @@ -107,14 +107,14 @@ private synchronized T executeWithRetry(ConnectionFunction func, Connecti throws SQLException { int attempt = 1; while (true) { - if (conn == null) { + if (!isConnectionCreated()) { LOGGER.debug("Trying to reconnect (attempt {}).", attempt); try { createConnection(); } catch (SQLException e) { - LOGGER.error("SQL Exception while trying to reconnect: " + e.getMessage(), e); - conn = null; + LOGGER.error("SQL Exception while trying to reconnect: {}", e.getMessage(), e); + close(); if (attempt >= maxRetryCount) { throw e; } @@ -145,14 +145,7 @@ private synchronized T executeWithRetry(ConnectionFunction func, Connecti // ignore } } - try { - conn.close(); - } - catch (Exception ex) { - // ignore - } - conn = null; - + close(); } } }