Skip to content

Commit

Permalink
1. try fix OutOfMemoryError when using database
Browse files Browse the repository at this point in the history
未经过测试,请勿使用
  • Loading branch information
RegadPoleCN committed Jul 3, 2024
1 parent e784645 commit e71d807
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class DataBase {
public static String mysql_host() {return Config.getConfigYaml().getString("database.settings.mysql.host");}
public static String mysql_port() {return Config.getConfigYaml().getString("database.settings.mysql.port");}
public static String mysql_database() {return Config.getConfigYaml().getString("database.settings.mysql.database");}
public static String mysql_username() {return Config.getConfigYaml().getString("database.settings.mysql.username");}
public static String mysql_user() {return Config.getConfigYaml().getString("database.settings.mysql.user");}
public static String mysql_password() {return Config.getConfigYaml().getString("database.settings.mysql.password");}
public static String mysql_parameters() {return Config.getConfigYaml().getString("database.settings.mysql.parameters");}
public static long pool_connectionTimeout() {return Config.getConfigYaml().getLong("database.settings.pool.connectionTimeout");}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,14 @@ public void onChannelMessageReceive(ChannelMessageEvent e) {
return;
}
if(admins.contains(senderID)) {
if (!WhitelistHelper.checkIDNotExist(PlayerName)) {
e.getMessage().reply("绑定失败,此ID已绑定用户"+DatabaseManager.getBindId(PlayerName, DataBase.type().toLowerCase(), PlumBot.getDatabase()));
return;
}
List<String> id = WhitelistHelper.addAndGet(PlayerName, String.valueOf(senderID), DataBase.type().toLowerCase(), PlumBot.getDatabase());
e.getMessage().reply("成功申请白名单,您目前的白名单为"+id);
PlumBot.getScheduler().runTaskAsynchronously(()->{
if (!WhitelistHelper.checkIDNotExist(PlayerName)) {
e.getMessage().reply("绑定失败,此ID已绑定用户"+DatabaseManager.getBindId(PlayerName, DataBase.type().toLowerCase(), PlumBot.getDatabase()));
return;
}
List<String> id = WhitelistHelper.addAndGet(PlayerName, String.valueOf(senderID), DataBase.type().toLowerCase(), PlumBot.getDatabase());
e.getMessage().reply("成功申请白名单,您目前的白名单为"+id);
});
return;
}
PlumBot.getScheduler().runTaskAsynchronously(() -> {
Expand All @@ -313,12 +315,14 @@ public void onChannelMessageReceive(ChannelMessageEvent e) {
return;
} else if (para.length==2) {
if(admins.contains(senderID)) {
if (!WhitelistHelper.checkIDNotExist(para[1])) {
e.getMessage().reply("绑定失败,此ID已绑定用户"+DatabaseManager.getBindId(para[1], DataBase.type().toLowerCase(), PlumBot.getDatabase()));
return;
}
List<String> id = WhitelistHelper.addAndGet(para[1], para[0], DataBase.type().toLowerCase(), PlumBot.getDatabase());
e.getMessage().reply("成功申请白名单,"+para[0]+"目前的白名单为"+id);
PlumBot.getScheduler().runTaskAsynchronously(()->{
if (!WhitelistHelper.checkIDNotExist(para[1])) {
e.getMessage().reply("绑定失败,此ID已绑定用户"+DatabaseManager.getBindId(para[1], DataBase.type().toLowerCase(), PlumBot.getDatabase()));
return;
}
List<String> id = WhitelistHelper.addAndGet(para[1], para[0], DataBase.type().toLowerCase(), PlumBot.getDatabase());
e.getMessage().reply("成功申请白名单,"+para[0]+"目前的白名单为"+id);
});
return;
}
}
Expand Down Expand Up @@ -456,10 +460,12 @@ public void onPrivateMessageReceive(PrivateMessageReceivedEvent e) {
public void onGroupDecreaseNotice(UserLeaveGuildEvent e) {
String userId = e.getUser().getId();
String groupId = e.getGuildId();
List<String> player = DatabaseManager.getBind(userId, DataBase.type().toLowerCase(), PlumBot.getDatabase());
if (player.isEmpty()) {
return;
}
DatabaseManager.removeBind(userId, DataBase.type().toLowerCase(), PlumBot.getDatabase());
PlumBot.getScheduler().runTaskAsynchronously(()->{
List<String> player = DatabaseManager.getBind(userId, DataBase.type().toLowerCase(), PlumBot.getDatabase());
if (player.isEmpty()) {
return;
}
DatabaseManager.removeBind(userId, DataBase.type().toLowerCase(), PlumBot.getDatabase());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,14 @@ public void onGroupMessageReceive(GroupMessage e){
return;
}
if(Config.getAdmins().contains(senderID)) {
if (!WhitelistHelper.checkIDNotExist(PlayerName)) {
bot.sendMsg(true, "绑定失败,此ID已绑定用户"+DatabaseManager.getBindId(PlayerName, DataBase.type().toLowerCase(), PlumBot.getDatabase()), groupID);
return;
}
List<String> id = WhitelistHelper.addAndGet(PlayerName, String.valueOf(senderID), DataBase.type().toLowerCase(), PlumBot.getDatabase());
bot.sendMsg(true, "成功申请白名单,您目前的白名单为"+id, groupID);
PlumBot.getScheduler().runTaskAsynchronously(()->{
if (!WhitelistHelper.checkIDNotExist(PlayerName)) {
bot.sendMsg(true, "绑定失败,此ID已绑定用户" + DatabaseManager.getBindId(PlayerName, DataBase.type().toLowerCase(), PlumBot.getDatabase()), groupID);
return;
}
List<String> id = WhitelistHelper.addAndGet(PlayerName, String.valueOf(senderID), DataBase.type().toLowerCase(), PlumBot.getDatabase());
bot.sendMsg(true, "成功申请白名单,您目前的白名单为"+id, groupID);
});
return;
}
PlumBot.getScheduler().runTaskAsynchronously(() -> {
Expand All @@ -339,12 +341,14 @@ public void onGroupMessageReceive(GroupMessage e){
return;
} else if (para.length==2) {
if(Config.getAdmins().contains(senderID)) {
if (!WhitelistHelper.checkIDNotExist(para[1])) {
bot.sendMsg(true, "绑定失败,此ID已绑定用户"+DatabaseManager.getBindId(para[1], DataBase.type().toLowerCase(), PlumBot.getDatabase()), groupID);
return;
}
List<String> id = WhitelistHelper.addAndGet(para[1], para[0], DataBase.type().toLowerCase(), PlumBot.getDatabase());
bot.sendMsg(true, "成功申请白名单,"+para[0]+"目前的白名单为"+id, groupID);
PlumBot.getScheduler().runTaskAsynchronously(()->{
if (!WhitelistHelper.checkIDNotExist(para[1])) {
bot.sendMsg(true, "绑定失败,此ID已绑定用户"+DatabaseManager.getBindId(para[1], DataBase.type().toLowerCase(), PlumBot.getDatabase()), groupID);
return;
}
List<String> id = WhitelistHelper.addAndGet(para[1], para[0], DataBase.type().toLowerCase(), PlumBot.getDatabase());
bot.sendMsg(true, "成功申请白名单,"+para[0]+"目前的白名单为"+id, groupID);
});
return;
}
}
Expand Down Expand Up @@ -461,11 +465,13 @@ public void onGroupMessageReceive(GroupMessage e){
public void onGroupDecreaseNotice(GroupDecreaseNotice e) {
long userId = e.getUserId();
long groupId = e.getGroupId();
List<String> player = DatabaseManager.getBind(String.valueOf(userId), DataBase.type().toLowerCase(), PlumBot.getDatabase());
if (player.isEmpty()) {
return;
}
DatabaseManager.removeBind(String.valueOf(userId), DataBase.type().toLowerCase(), PlumBot.getDatabase());
PlumBot.getScheduler().runTaskAsynchronously(() -> {
List<String> player = DatabaseManager.getBind(String.valueOf(userId), DataBase.type().toLowerCase(), PlumBot.getDatabase());
if (player.isEmpty()) {
return;
}
DatabaseManager.removeBind(String.valueOf(userId), DataBase.type().toLowerCase(), PlumBot.getDatabase());
});
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import me.regadpole.plumbot.PlumBot;
import me.regadpole.plumbot.config.DataBase;

import javax.annotation.Nullable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -164,8 +163,15 @@ public static void removeBind(String qq, int num, String mode, Database db) {
return;
}
String id = resultSet.getString("id");
String delete = "DELETE FROM whitelist WHERE qq=" + qq+" AND id='" + id + "';";
statement.executeUpdate(delete);
if (id == null) {
resultSet.close();
statement.close();
connection.close();
break;
}else {
String delete = "DELETE FROM whitelist WHERE id='" + id + "';";
statement.executeUpdate(delete);
}
resultSet.close();
statement.close();
connection.close();
Expand All @@ -187,8 +193,14 @@ public static void removeBind(String qq, int num, String mode, Database db) {
return;
}
String id = resultSet.getString("id");
String delete = "DELETE FROM whitelist WHERE qq=" + qq+" AND id='" + id + "';";
connection.prepareStatement(delete).executeUpdate();
if (id == null) {
resultSet.close();
connection.close();
break;
}else {
String delete = "DELETE FROM whitelist WHERE id='" + id + "';";
connection.prepareStatement(delete).executeUpdate();
}
resultSet.close();
connection.close();
break;
Expand Down Expand Up @@ -307,7 +319,14 @@ public static List<String> getBind(String qq,String mode, Database db) {
resultSet.next();
}
do {
id.add(resultSet.getString("id"));
String tempId = resultSet.getString("id");
if (tempId == null){
resultSet.close();
connection.close();
break;
} else {
id.add(tempId);
}
resultSet.next();
}while(!resultSet.isAfterLast());
resultSet.close();
Expand All @@ -325,7 +344,15 @@ public static List<String> getBind(String qq,String mode, Database db) {
resultSet.next();
}
do {
id.add(resultSet.getString("id"));
String tempId = resultSet.getString("id");
if (tempId == null){
resultSet.close();
statement.close();
connection.close();
break;
} else {
id.add(tempId);
}
resultSet.next();
}while(!resultSet.isAfterLast());
resultSet.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void initialize() {
config.setDriverClassName(driver);
config.setPoolName("PlumBot-MySQL");
config.setJdbcUrl("jdbc:mysql://" + DataBase.mysql_host() +":"+ DataBase.mysql_port() + "/" + DataBase.mysql_database() + DataBase.mysql_parameters());
config.setUsername(DataBase.mysql_username());
config.setUsername(DataBase.mysql_user());
config.setPassword(DataBase.mysql_password());
if (!DataBase.pool_connectionTestQuery().isEmpty()){
config.setConnectionTestQuery(DataBase.pool_connectionTestQuery());
Expand Down

0 comments on commit e71d807

Please sign in to comment.