Skip to content

Commit

Permalink
Added HikariCP, 4.2 -> 5
Browse files Browse the repository at this point in the history
  • Loading branch information
NightMirror authored and NightMirror committed Mar 17, 2023
1 parent e26c719 commit 9509d3e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ru.nightmirror</groupId>
<artifactId>WhitelistByTime</artifactId>
<version>4.1</version>
<version>5.0</version>

<repositories>
<repository>
Expand Down Expand Up @@ -57,6 +57,13 @@
<version>2.11.2</version>
<scope>provided</scope>
</dependency>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 org.bukkit.entity.Player;
import ru.nightmirror.wlbytime.interfaces.database.IDatabase;
Expand All @@ -9,9 +11,9 @@
import ru.nightmirror.wlbytime.shared.api.events.PlayerAddedToWhitelistEvent;
import ru.nightmirror.wlbytime.shared.api.events.PlayerRemovedFromWhitelistEvent;

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.ArrayList;
Expand All @@ -20,8 +22,8 @@
import java.util.logging.Logger;

public class Database implements IDatabase {
private Boolean useUserAndPassword;
private String conStr;
private HikariConfig config;
private HikariDataSource source;
private final WhitelistByTime plugin;
private final static Logger LOG = Logger.getLogger("WhitelistByTime");

Expand All @@ -31,8 +33,13 @@ public Database(WhitelistByTime plugin) {
}

private void enable() {
conStr = getStringSource();
useUserAndPassword = getConfigBoolean("use-user-and-password", false);
config = new HikariConfig();
config.setJdbcUrl(getStringURL());

if (getConfigBoolean("use-user-and-password", false)) {
config.setUsername(getConfigString("user"));
config.setPassword(getConfigString("password"));
}

createTable();
}
Expand All @@ -59,17 +66,21 @@ private void createTable() {
}
}

private String getStringSource() {
private String getStringURL() {
final String type = getConfigString("type", "sqlite");
if (type.equalsIgnoreCase("sqlite") || type.equalsIgnoreCase("h2"))
return "jdbc:" + type + ":" + new File(plugin.getDataFolder(), "database.db").getAbsolutePath();

return "jdbc:" + type + "://" + getConfigString("address") + File.separator + 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public PlaceholderHook(IDatabase database, Plugin plugin) {

@Override
public @NotNull String getVersion() {
return "4.0";
return "5.0";
}

@Override
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: 4.1
version: 5
author: NightMirror
description: Adding a player to the whitelist for a time
api-version: 1.16
Expand Down

0 comments on commit 9509d3e

Please sign in to comment.