Skip to content

Commit

Permalink
Added HikariCP, 4.2 -> 5. Second attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
NightMirror committed Mar 17, 2023
1 parent 572bbb4 commit 089cf73
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
34 changes: 22 additions & 12 deletions src/main/java/ru/nightmirror/wlbytime/shared/database/Database.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
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;
import ru.nightmirror.wlbytime.shared.WhitelistByTime;
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");

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

Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -184,7 +198,7 @@ public Map<String, Long> getAll() {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)
) {
Map<String, Long> players = new HashMap<String, Long>();
Map<String, Long> players = new HashMap<>();

while (resultSet.next()) {
String nickname = resultSet.getString("nickname");
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 089cf73

Please sign in to comment.