From 089cf7340c062fa76afc850382d85ea91d3c1702 Mon Sep 17 00:00:00 2001 From: NightMirror Date: Fri, 17 Mar 2023 15:31:35 +0300 Subject: [PATCH] Added HikariCP, 4.2 -> 5. Second attempt --- .../wlbytime/shared/database/Database.java | 34 ++++++++++++------- src/main/resources/plugin.yml | 2 +- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/ru/nightmirror/wlbytime/shared/database/Database.java b/src/main/java/ru/nightmirror/wlbytime/shared/database/Database.java index 44adb0c..6ddf369 100644 --- a/src/main/java/ru/nightmirror/wlbytime/shared/database/Database.java +++ b/src/main/java/ru/nightmirror/wlbytime/shared/database/Database.java @@ -1,5 +1,7 @@ package ru.nightmirror.wlbytime.shared.database; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; import org.bukkit.Bukkit; import ru.nightmirror.wlbytime.interfaces.database.IDatabase; import ru.nightmirror.wlbytime.misc.convertors.TimeConvertor; @@ -7,18 +9,20 @@ import ru.nightmirror.wlbytime.shared.api.events.PlayerAddedToWhitelistEvent; import ru.nightmirror.wlbytime.shared.common.Checker; +import javax.annotation.Nullable; import java.io.File; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; -import java.util.*; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; import java.util.logging.Logger; public class Database implements IDatabase { - private Boolean useUserAndPassword; + private HikariConfig config; + private HikariDataSource source; private String DBTable; - private String conStr; private final WhitelistByTime plugin; private final static Logger LOG = Logger.getLogger("WhitelistByTime"); @@ -27,10 +31,16 @@ public Database(WhitelistByTime plugin) { enable(); } private void enable() { - conStr = getStringSource(); - useUserAndPassword = getConfigBoolean("use-user-and-password", false); DBTable = getConfigString("table", "whitelist"); + config = new HikariConfig(); + config.setJdbcUrl(getStringSource()); + + if (getConfigBoolean("use-user-and-password", false)) { + config.setUsername(getConfigString("user")); + config.setPassword(getConfigString("password")); + } + createTable(); } @@ -64,9 +74,13 @@ private String getStringSource() { return "jdbc:" + type + "://" + getConfigString("address") + "/" + getConfigString("name"); } + @Nullable private Connection getConnection() { try { - return useUserAndPassword ? DriverManager.getConnection(conStr, getConfigString("user"), getConfigString("password")) : DriverManager.getConnection(conStr); + if (source == null || source.isClosed()) { + source = new HikariDataSource(config); + } + return source.getConnection(); } catch (Exception exception) { LOG.severe("Can't create connection: " + exception.getMessage()); } @@ -184,7 +198,7 @@ public Map getAll() { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query) ) { - Map players = new HashMap(); + Map players = new HashMap<>(); while (resultSet.next()) { String nickname = resultSet.getString("nickname"); @@ -207,10 +221,6 @@ private String getConfigString(String path, String def) { return plugin.getConfig().getString(path, def); } - private Boolean getConfigBoolean(String path) { - return getConfigBoolean(path, false); - } - private Boolean getConfigBoolean(String path, Boolean def) { return plugin.getConfig().getBoolean(path, def); } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 71439fa..50e79ef 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: WhitelistByTime main: ru.nightmirror.wlbytime.shared.WhitelistByTime -version: 5 +version: 5.0 author: NightMirror description: Adding a player to the whitelist for a time api-version: 1.16