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:
Aust1n46 2021-04-09 17:19:08 -05:00
parent c663c49e9c
commit 8a6a4d61f2
2 changed files with 44 additions and 39 deletions

View File

@ -228,7 +228,9 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener {
MineverseChatAPI.addMineverseChatOnlinePlayerToMap(mcp);
}
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
Database.initializeMySQL();
});
commands.put("broadcast", new Broadcast("broadcast"));
commands.put("channel", new Channel("channel"));

View File

@ -13,6 +13,7 @@ import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import mineverse.Aust1n46.chat.MineverseChat;
import mineverse.Aust1n46.chat.utilities.Format;
/**
* Initializes and handles writing to the chat logging database.
@ -21,6 +22,7 @@ public class Database {
private static HikariDataSource dataSource = null;
public static void initializeMySQL() {
try {
ConfigurationSection mysqlConfig = MineverseChat.getInstance().getConfig().getConfigurationSection("mysql");
if (mysqlConfig.getBoolean("enabled", false)) {
String host = mysqlConfig.getString("host");
@ -31,8 +33,10 @@ public class Database {
final HikariConfig config = new HikariConfig();
// 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:mysql://%s:%d/%s?autoReconnect=true&useSSL=false", host, 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?autoReconnect=true&useSSL=false", host,
port, database);
config.setJdbcUrl(jdbcUrl);
config.setUsername(user);
config.setPassword(password);
@ -40,16 +44,16 @@ public class Database {
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
dataSource = new HikariDataSource(config);
final String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS VentureChat " +
"(ID SERIAL PRIMARY KEY, ChatTime TEXT, UUID TEXT, Name TEXT, " +
"Server TEXT, Channel TEXT, Text TEXT, Type TEXT)";
try (final Connection conn = dataSource.getConnection();
final PreparedStatement statement = conn.prepareStatement(SQL_CREATE_TABLE)) {
final String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS VentureChat "
+ "(ID SERIAL PRIMARY KEY, ChatTime TEXT, UUID TEXT, Name TEXT, "
+ "Server TEXT, Channel TEXT, Text TEXT, Type TEXT)";
final Connection conn = dataSource.getConnection();
final PreparedStatement statement = conn.prepareStatement(SQL_CREATE_TABLE);
statement.executeUpdate();
}
catch (SQLException e) {
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,7 +61,8 @@ public class Database {
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();
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@ -65,9 +70,8 @@ public class Database {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try (final Connection conn = dataSource.getConnection();
final PreparedStatement statement = conn.prepareStatement(
"INSERT INTO VentureChat " +
"(ChatTime, UUID, Name, Server, Channel, Text, Type) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)")) {
"INSERT INTO VentureChat " + "(ChatTime, UUID, Name, Server, Channel, Text, Type) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?)")) {
statement.setString(1, date);
statement.setString(2, uuid);
statement.setString(3, name);
@ -76,8 +80,7 @@ public class Database {
statement.setString(6, text);
statement.setString(7, type);
statement.executeUpdate();
}
catch(SQLException error) {
} catch (SQLException error) {
error.printStackTrace();
}
});