Skip to content
Open
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
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'java'
id 'jacoco'
id 'com.github.kt3k.coveralls' version '2.12.2'
id "com.github.johnrengelman.shadow" version "8.1.1"
}

group 'net.simplyvanilla'
Expand Down Expand Up @@ -31,6 +32,8 @@ dependencies {
compileOnly 'io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT'
compileOnly 'io.github.miniplaceholders:miniplaceholders-api:2.2.3'

implementation 'com.github.chris2018998:beecp:4.0.1'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2'
testImplementation 'com.github.seeseemelk:MockBukkit-v1.20:3.78.0'
Expand All @@ -42,6 +45,17 @@ test {
useJUnitPlatform()
}

tasks {
shadowJar {
archiveClassifier.set('')
relocate('org.stone.beecp', 'net.simplyvanilla.dependencies.beecp')
relocate('org.stone.tools', 'net.simplyvanilla.dependencies.tools')
}
build {
dependsOn shadowJar
}
}

jacoco {
toolVersion = '0.8.11' // specify the desired version
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.simplyvanilla.simplyrank.database.sql;

import net.simplyvanilla.simplyrank.SimplyRankPlugin;
import org.stone.beecp.BeeDataSource;
import org.stone.beecp.BeeDataSourceConfig;

import java.sql.*;

Expand All @@ -13,31 +15,27 @@ public class MySqlClient {
private final String username;
private final String password;

private Connection connection;
private BeeDataSource dataSource;

public MySqlClient(String url, String user, String password) {
this.url = url;
this.username = user;
this.password = password;

connect();
initTables();
this.connect();
this.initTables();
}

public void connect() {
try {
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
SimplyRankPlugin.getInstance().getSLF4JLogger().error("Could not connect to MySQL database", e);
}
var config = new BeeDataSourceConfig();
config.setJdbcUrl(this.url);
config.setUsername(this.username);
config.setPassword(this.password);
this.dataSource = new BeeDataSource(config);
}

public void close() {
try {
if (connection != null) connection.close();
} catch (SQLException e) {
SimplyRankPlugin.getInstance().getSLF4JLogger().error("Could not close MySQL connection", e);
}
this.dataSource.close();
}

public void update(PreparedStatement statement) throws SQLException {
Expand All @@ -55,7 +53,7 @@ public ResultSet query(PreparedStatement statement) throws SQLException {
}

private void executeRawStatement(String cmd) throws SQLException {
try (Statement st = connection.createStatement()) {
try (Connection connection = this.dataSource.getConnection(); Statement st = connection.createStatement()) {
st.execute(cmd);
}
}
Expand Down Expand Up @@ -127,7 +125,9 @@ PRIMARY KEY (`id`),
}

public PreparedStatement prepareStatement(String qry) throws SQLException {
return connection.prepareStatement(qry);
try(Connection connection = this.dataSource.getConnection()) {
return connection.prepareStatement(qry);
}
}

public PreparedStatement prepareStatement(String qry, String... parameters) throws SQLException {
Expand Down