mirror of
https://github.com/Aust1n46/VentureChat.git
synced 2025-05-23 10:39:05 +00:00
Fix leaky database connection
This commit is contained in:
parent
2e4b748845
commit
65bc1ab49c
@ -291,7 +291,6 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener {
|
|||||||
String user = mysqlConfig.getString("user");
|
String user = mysqlConfig.getString("user");
|
||||||
String password = mysqlConfig.getString("password");
|
String password = mysqlConfig.getString("password");
|
||||||
db = new MySQL(host, port, database, user, password);
|
db = new MySQL(host, port, database, user, password);
|
||||||
db.init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
commands.put("broadcast", new Broadcast("broadcast"));
|
commands.put("broadcast", new Broadcast("broadcast"));
|
||||||
|
@ -15,8 +15,6 @@ public abstract class Database {
|
|||||||
|
|
||||||
protected HikariDataSource dataSource = null;
|
protected HikariDataSource dataSource = null;
|
||||||
|
|
||||||
public abstract void init();
|
|
||||||
|
|
||||||
public void writeVentureChat(String time, String uuid, String name, String server, String channel, String text, String type) {
|
public void writeVentureChat(String time, String uuid, String name, String server, String channel, String text, String type) {
|
||||||
MineverseChat plugin = MineverseChat.getInstance();
|
MineverseChat plugin = MineverseChat.getInstance();
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||||
|
@ -1,31 +1,17 @@
|
|||||||
package mineverse.Aust1n46.chat.database;
|
package mineverse.Aust1n46.chat.database;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
|
||||||
//This class initializes the plugins connection to the MySQL database if it's enabled.
|
//This class initializes the plugin's connection to the MySQL database if it's enabled.
|
||||||
public class MySQL extends Database {
|
public class MySQL extends Database {
|
||||||
private final String user;
|
|
||||||
private final String database;
|
|
||||||
private final String password;
|
|
||||||
private final int port;
|
|
||||||
private final String hostname;
|
|
||||||
|
|
||||||
public MySQL(String hostname, int port, String database, String username, String password) {
|
public MySQL(String hostname, int port, String database, String user, String password) {
|
||||||
this.hostname = hostname;
|
final HikariConfig config = new HikariConfig();
|
||||||
this.port = port;
|
|
||||||
this.database = database;
|
|
||||||
this.user = username;
|
|
||||||
this.password = password;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init() {
|
|
||||||
HikariConfig config = new HikariConfig();
|
|
||||||
//config.setDriverClassName(org.postgresql.Driver.class.getName());
|
//config.setDriverClassName(org.postgresql.Driver.class.getName());
|
||||||
//final String jdbcUrl = String.format("jdbc:postgresql://%s:%d/%s", hostname, port, database);
|
//final String jdbcUrl = String.format("jdbc:postgresql://%s:%d/%s", hostname, port, database);
|
||||||
final String jdbcUrl = String.format("jdbc:mysql://%s:%d/%s", hostname, port, database);
|
final String jdbcUrl = String.format("jdbc:mysql://%s:%d/%s", hostname, port, database);
|
||||||
@ -36,13 +22,14 @@ public class MySQL extends Database {
|
|||||||
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||||
dataSource = new HikariDataSource(config);
|
dataSource = new HikariDataSource(config);
|
||||||
try {
|
final String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS VentureChat " +
|
||||||
Connection conn = dataSource.getConnection();
|
|
||||||
Statement statement = conn.createStatement();
|
|
||||||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS VentureChat " +
|
|
||||||
"(ID SERIAL PRIMARY KEY, ChatTime TEXT, UUID TEXT, Name TEXT, " +
|
"(ID SERIAL PRIMARY KEY, ChatTime TEXT, UUID TEXT, Name TEXT, " +
|
||||||
"Server TEXT, Channel TEXT, Text TEXT, Type TEXT)");
|
"Server TEXT, Channel TEXT, Text TEXT, Type TEXT)";
|
||||||
} catch (SQLException e) {
|
try (final Connection conn = dataSource.getConnection();
|
||||||
|
final PreparedStatement statement = conn.prepareStatement(SQL_CREATE_TABLE)) {
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,14 +10,8 @@ import mineverse.Aust1n46.chat.MineverseChat;
|
|||||||
|
|
||||||
//This class initializes the connection to a SQLite database, which has no implementations currently in the plugin.
|
//This class initializes the connection to a SQLite database, which has no implementations currently in the plugin.
|
||||||
public class SQLite extends Database {
|
public class SQLite extends Database {
|
||||||
private final String dbLocation;
|
|
||||||
|
|
||||||
public SQLite(String dbLocation) {
|
public SQLite(String dbLocation) {
|
||||||
this.dbLocation = dbLocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init() {
|
|
||||||
File dataFolder = MineverseChat.getInstance().getDataFolder();
|
File dataFolder = MineverseChat.getInstance().getDataFolder();
|
||||||
if (!dataFolder.exists()) dataFolder.mkdirs();
|
if (!dataFolder.exists()) dataFolder.mkdirs();
|
||||||
File databaseFile = new File(dataFolder, dbLocation);
|
File databaseFile = new File(dataFolder, dbLocation);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user