Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix][Registry] Fix mysql registry error #250

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading