mirror of
https://github.com/Aust1n46/VentureChat.git
synced 2025-05-23 10:39:05 +00:00
Load database async and add exception handling.
Hikari seems to print it's own stacktrace even when you catch the exception.
This commit is contained in:
parent
c663c49e9c
commit
8a6a4d61f2
@ -228,7 +228,9 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener {
|
|||||||
MineverseChatAPI.addMineverseChatOnlinePlayerToMap(mcp);
|
MineverseChatAPI.addMineverseChatOnlinePlayerToMap(mcp);
|
||||||
}
|
}
|
||||||
|
|
||||||
Database.initializeMySQL();
|
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
|
||||||
|
Database.initializeMySQL();
|
||||||
|
});
|
||||||
|
|
||||||
commands.put("broadcast", new Broadcast("broadcast"));
|
commands.put("broadcast", new Broadcast("broadcast"));
|
||||||
commands.put("channel", new Channel("channel"));
|
commands.put("channel", new Channel("channel"));
|
||||||
|
@ -13,6 +13,7 @@ import com.zaxxer.hikari.HikariConfig;
|
|||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
|
||||||
import mineverse.Aust1n46.chat.MineverseChat;
|
import mineverse.Aust1n46.chat.MineverseChat;
|
||||||
|
import mineverse.Aust1n46.chat.utilities.Format;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes and handles writing to the chat logging database.
|
* Initializes and handles writing to the chat logging database.
|
||||||
@ -21,35 +22,38 @@ public class Database {
|
|||||||
private static HikariDataSource dataSource = null;
|
private static HikariDataSource dataSource = null;
|
||||||
|
|
||||||
public static void initializeMySQL() {
|
public static void initializeMySQL() {
|
||||||
ConfigurationSection mysqlConfig = MineverseChat.getInstance().getConfig().getConfigurationSection("mysql");
|
try {
|
||||||
if (mysqlConfig.getBoolean("enabled", false)) {
|
ConfigurationSection mysqlConfig = MineverseChat.getInstance().getConfig().getConfigurationSection("mysql");
|
||||||
String host = mysqlConfig.getString("host");
|
if (mysqlConfig.getBoolean("enabled", false)) {
|
||||||
int port = mysqlConfig.getInt("port");
|
String host = mysqlConfig.getString("host");
|
||||||
String database = mysqlConfig.getString("database");
|
int port = mysqlConfig.getInt("port");
|
||||||
String user = mysqlConfig.getString("user");
|
String database = mysqlConfig.getString("database");
|
||||||
String password = mysqlConfig.getString("password");
|
String user = mysqlConfig.getString("user");
|
||||||
|
String password = mysqlConfig.getString("password");
|
||||||
|
|
||||||
final HikariConfig config = new HikariConfig();
|
final 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,
|
||||||
final String jdbcUrl = String.format("jdbc:mysql://%s:%d/%s?autoReconnect=true&useSSL=false", host, port, database);
|
// port, database);
|
||||||
config.setJdbcUrl(jdbcUrl);
|
final String jdbcUrl = String.format("jdbc:mysql://%s:%d/%s?autoReconnect=true&useSSL=false", host,
|
||||||
config.setUsername(user);
|
port, database);
|
||||||
config.setPassword(password);
|
config.setJdbcUrl(jdbcUrl);
|
||||||
config.addDataSourceProperty("cachePrepStmts", "true");
|
config.setUsername(user);
|
||||||
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
config.setPassword(password);
|
||||||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
config.addDataSourceProperty("cachePrepStmts", "true");
|
||||||
dataSource = new HikariDataSource(config);
|
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||||
final String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS VentureChat " +
|
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||||
"(ID SERIAL PRIMARY KEY, ChatTime TEXT, UUID TEXT, Name TEXT, " +
|
dataSource = new HikariDataSource(config);
|
||||||
"Server TEXT, Channel TEXT, Text TEXT, Type TEXT)";
|
final String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS VentureChat "
|
||||||
try (final Connection conn = dataSource.getConnection();
|
+ "(ID SERIAL PRIMARY KEY, ChatTime TEXT, UUID TEXT, Name TEXT, "
|
||||||
final PreparedStatement statement = conn.prepareStatement(SQL_CREATE_TABLE)) {
|
+ "Server TEXT, Channel TEXT, Text TEXT, Type TEXT)";
|
||||||
statement.executeUpdate();
|
final Connection conn = dataSource.getConnection();
|
||||||
}
|
final PreparedStatement statement = conn.prepareStatement(SQL_CREATE_TABLE);
|
||||||
catch (SQLException e) {
|
statement.executeUpdate();
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
} catch (Exception exception) {
|
||||||
|
Bukkit.getConsoleSender().sendMessage(
|
||||||
|
Format.FormatStringAll("&8[&eVentureChat&8]&c - Database could not be loaded. Is it running?"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,17 +61,17 @@ public class Database {
|
|||||||
return dataSource != null;
|
return dataSource != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeVentureChat(String uuid, String name, String server, String channel, String text, String type) {
|
public static void writeVentureChat(String uuid, String name, String server, String channel, String text,
|
||||||
|
String type) {
|
||||||
MineverseChat plugin = MineverseChat.getInstance();
|
MineverseChat plugin = MineverseChat.getInstance();
|
||||||
Calendar currentDate = Calendar.getInstance();
|
Calendar currentDate = Calendar.getInstance();
|
||||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
String date = formatter.format(currentDate.getTime());
|
String date = formatter.format(currentDate.getTime());
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||||
try(final Connection conn = dataSource.getConnection();
|
try (final Connection conn = dataSource.getConnection();
|
||||||
final PreparedStatement statement = conn.prepareStatement(
|
final PreparedStatement statement = conn.prepareStatement(
|
||||||
"INSERT INTO VentureChat " +
|
"INSERT INTO VentureChat " + "(ChatTime, UUID, Name, Server, Channel, Text, Type) "
|
||||||
"(ChatTime, UUID, Name, Server, Channel, Text, Type) " +
|
+ "VALUES (?, ?, ?, ?, ?, ?, ?)")) {
|
||||||
"VALUES (?, ?, ?, ?, ?, ?, ?)")) {
|
|
||||||
statement.setString(1, date);
|
statement.setString(1, date);
|
||||||
statement.setString(2, uuid);
|
statement.setString(2, uuid);
|
||||||
statement.setString(3, name);
|
statement.setString(3, name);
|
||||||
@ -76,8 +80,7 @@ public class Database {
|
|||||||
statement.setString(6, text);
|
statement.setString(6, text);
|
||||||
statement.setString(7, type);
|
statement.setString(7, type);
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
}
|
} catch (SQLException error) {
|
||||||
catch(SQLException error) {
|
|
||||||
error.printStackTrace();
|
error.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user