From 2f3ad0e52c0431642afe7b8c0287446dc89372da Mon Sep 17 00:00:00 2001 From: zixi0825 Date: Wed, 18 Oct 2023 23:37:25 +0800 Subject: [PATCH] [Fix][Registry] Fix mysql registry error --- .../io/datavines/registry/plugin/MysqlRegistry.java | 6 ++++-- .../registry/plugin/MysqlServerStateManager.java | 10 +++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/datavines-registry/datavines-registry-plugins/datavines-registry-mysql/src/main/java/io/datavines/registry/plugin/MysqlRegistry.java b/datavines-registry/datavines-registry-plugins/datavines-registry-mysql/src/main/java/io/datavines/registry/plugin/MysqlRegistry.java index 1298ac7b9..3e5b9d249 100644 --- a/datavines-registry/datavines-registry-plugins/datavines-registry-mysql/src/main/java/io/datavines/registry/plugin/MysqlRegistry.java +++ b/datavines-registry/datavines-registry-plugins/datavines-registry-mysql/src/main/java/io/datavines/registry/plugin/MysqlRegistry.java @@ -17,6 +17,7 @@ package io.datavines.registry.plugin; import io.datavines.registry.api.*; +import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,6 +26,7 @@ import java.util.List; import java.util.Properties; +@Slf4j public class MysqlRegistry implements Registry { private static final Logger logger = LoggerFactory.getLogger(MysqlRegistry.class); @@ -75,7 +77,7 @@ public void subscribe(String key, SubscribeListener subscribeListener) { try { mysqlServerStateManager.registry(subscribeListener); } catch (Exception e){ - e.printStackTrace(); + logger.warn("subscribe error: ", e); } } @@ -85,7 +87,7 @@ public void unSubscribe(String key) { try { mysqlServerStateManager.unRegistry(); } catch (Exception e){ - e.printStackTrace(); + logger.warn("unSubscribe error: ", e); } } diff --git a/datavines-registry/datavines-registry-plugins/datavines-registry-mysql/src/main/java/io/datavines/registry/plugin/MysqlServerStateManager.java b/datavines-registry/datavines-registry-plugins/datavines-registry-mysql/src/main/java/io/datavines/registry/plugin/MysqlServerStateManager.java index fa08b47d9..84fc7e9ed 100644 --- a/datavines-registry/datavines-registry-plugins/datavines-registry-mysql/src/main/java/io/datavines/registry/plugin/MysqlServerStateManager.java +++ b/datavines-registry/datavines-registry-plugins/datavines-registry-mysql/src/main/java/io/datavines/registry/plugin/MysqlServerStateManager.java @@ -134,6 +134,7 @@ private void executeInsert(ServerInfo serverInfo) throws SQLException { preparedStatement.setString(1, serverInfo.getHost()); preparedStatement.setInt(2, serverInfo.getServerPort()); preparedStatement.executeUpdate(); + preparedStatement.close(); } private void executeUpdate(ServerInfo serverInfo) throws SQLException { @@ -143,6 +144,7 @@ private void executeUpdate(ServerInfo serverInfo) throws SQLException { preparedStatement.setString(2, serverInfo.getHost()); preparedStatement.setInt(3, serverInfo.getServerPort()); preparedStatement.executeUpdate(); + preparedStatement.close(); } private void executeDelete(ServerInfo serverInfo) throws SQLException { @@ -151,6 +153,7 @@ private void executeDelete(ServerInfo serverInfo) throws SQLException { preparedStatement.setString(1, serverInfo.getHost()); preparedStatement.setInt(2, serverInfo.getServerPort()); preparedStatement.executeUpdate(); + preparedStatement.close(); } private boolean isExists(ServerInfo serverInfo) throws SQLException { @@ -161,10 +164,12 @@ private boolean isExists(ServerInfo serverInfo) throws SQLException { ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet == null) { + preparedStatement.close(); return false; } boolean result = resultSet.first(); resultSet.close(); + preparedStatement.close(); return result; } @@ -174,17 +179,20 @@ private ConcurrentHashMap fetchServers() throws SQLException ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet == null) { + preparedStatement.close(); return null; } ConcurrentHashMap map = new ConcurrentHashMap<>(); - while (resultSet.next()){ + while (resultSet.next()) { String host = resultSet.getString("host"); int port = resultSet.getInt("port"); Timestamp updateTime = resultSet.getTimestamp("update_time"); Timestamp createTime = resultSet.getTimestamp("create_time"); map.put(host + ":" + port, new ServerInfo(host, port, createTime, updateTime)); } + resultSet.close(); + preparedStatement.close(); return map; }