Adds sanity checks

This commit is contained in:
zontreck 2024-06-18 14:45:41 -07:00
parent a85c032253
commit 1ad275a970
2 changed files with 25 additions and 1 deletions

View file

@ -53,7 +53,7 @@ mod_name=Zontreck's Library Mod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3 mod_license=GPLv3
# The mod version. See https://semver.org/ # The mod version. See https://semver.org/
mod_version=1201.13.043024.1530 mod_version=1201.13.050624.1254
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -14,6 +14,12 @@ public class DatabaseWrapper {
public static DatabaseWrapper get() { public static DatabaseWrapper get() {
if (instance == null) if (instance == null)
start(); start();
try {
instance.sanityCheck();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return instance; return instance;
} }
@ -42,6 +48,18 @@ public class DatabaseWrapper {
} }
} }
private void restart() {
LibZontreck.LOGGER.info("Reconnecting to database...");
LibZontreck.LOGGER.info("jdbc:db ://" + ServerConfig.database.user + "@" + ServerConfig.database.host + "/" + ServerConfig.database.database);
try {
instance.connect(ServerConfig.database.host, ServerConfig.database.user, ServerConfig.database.password, ServerConfig.database.database);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void connect(String url, String username, String password, String database) throws SQLException { public void connect(String url, String username, String password, String database) throws SQLException {
if(database.isBlank()) if(database.isBlank())
{ {
@ -85,6 +103,12 @@ public class DatabaseWrapper {
} }
} }
private void sanityCheck() throws SQLException {
if(connection.isClosed() || connection == null) {
restart();
}
}
public ResultSet executeQuery(String query) throws SQLException { public ResultSet executeQuery(String query) throws SQLException {
if (connection == null) { if (connection == null) {
throw new SQLException("Connection not established."); throw new SQLException("Connection not established.");