Skip to content

Commit

Permalink
[Fix][Registry] Fix mysql registry error (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
zixi0825 authored Oct 18, 2023
1 parent 1367c82 commit 6c97861
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

}
Expand All @@ -85,7 +87,7 @@ public void unSubscribe(String key) {
try {
mysqlServerStateManager.unRegistry();
} catch (Exception e){
e.printStackTrace();
logger.warn("unSubscribe error: ", e);
}
}

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

Expand All @@ -174,17 +179,20 @@ private ConcurrentHashMap<String, ServerInfo> fetchServers() throws SQLException
ResultSet resultSet = preparedStatement.executeQuery();

if (resultSet == null) {
preparedStatement.close();
return null;
}

ConcurrentHashMap<String, ServerInfo> 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;
}

Expand Down

0 comments on commit 6c97861

Please sign in to comment.