Skip to content

Commit

Permalink
fix: Don't catch exceptions when initializing the database
Browse files Browse the repository at this point in the history
Catching the exception here circumvents the error handing in `SongodaPlugin#onEnable()`.
+ catching the errors without throwing a new one leads to plugins continuing their initialization
before being disabled.
Poluting the server log with additional exceptions and everything because something is broken but surpressed
  • Loading branch information
SpraxDev committed Oct 23, 2023
1 parent 4816438 commit 2f6d21c
Showing 1 changed file with 34 additions and 39 deletions.
73 changes: 34 additions & 39 deletions Core/src/main/java/com/craftaro/core/database/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -66,53 +67,49 @@ public class DataManager {
}

public DataManager(SongodaPlugin plugin, List<DataMigration> migrations) {
this.plugin = plugin;
this.migrations = migrations;
this.databaseConfig = plugin.getDatabaseConfig();
load(null);
this(plugin, migrations, null);
}

public DataManager(SongodaPlugin plugin, List<DataMigration> migrations, DatabaseType forcedType) {
this.plugin = plugin;
this.migrations = migrations;
this.databaseConfig = plugin.getDatabaseConfig();
load(forcedType);
}

private void load(DatabaseType forcedType) {
try {
String databaseType = databaseConfig.getString("Connection Settings.Type").toUpperCase();
if (forcedType != null) {
databaseType = forcedType.name();
load(forcedType);
} catch (Exception ex) {
// FIXME: This try-catch exists for backwards-compatibility reasons (I basically don't want to invest the necessary time to do it properly)
throw new RuntimeException(ex);
}
}

private void load(DatabaseType forcedType) throws SQLException {
String databaseType = databaseConfig.getString("Connection Settings.Type").toUpperCase();
if (forcedType != null) {
databaseType = forcedType.name();
}
switch (databaseType) {
case "MYSQL": {
this.databaseConnector = new MySQLConnector(plugin, databaseConfig);
break;
}
switch (databaseType) {
case "MYSQL": {
this.databaseConnector = new MySQLConnector(plugin, databaseConfig);
break;
}
case "MARIADB": {
this.databaseConnector = new MariaDBConnector(plugin, databaseConfig);
break;
}
case "SQLITE": {
this.databaseConnector = new SQLiteConnector(plugin);
break;
}
default: {
//H2
this.databaseConnector = new H2Connector(plugin);
break;
}
case "MARIADB": {
this.databaseConnector = new MariaDBConnector(plugin, databaseConfig);
break;
}
case "SQLITE": {
this.databaseConnector = new SQLiteConnector(plugin);
break;
}
default: {
this.databaseConnector = new H2Connector(plugin);
break;
}
this.type = databaseConnector.getType();
this.plugin.getLogger().info("Data handler connected using " + databaseConnector.getType().name() + ".");

runMigrations();
} catch (Exception ex) {
this.plugin.getLogger().severe("Fatal error trying to connect to database. Please make sure all your connection settings are correct and try again. Plugin has been disabled.");
ex.printStackTrace();
Bukkit.getPluginManager().disablePlugin(this.plugin);
}
this.type = databaseConnector.getType();
this.plugin.getLogger().info("Data handler connected using " + databaseConnector.getType().name() + ".");

runMigrations();
}

/**
Expand Down Expand Up @@ -142,7 +139,7 @@ public String getTablePrefix() {
/**
* Runs any needed data migrations
*/
public void runMigrations() {
public void runMigrations() throws SQLException {
try (Connection connection = this.databaseConnector.getConnection()) {
int currentMigration = -1;
boolean migrationsExist;
Expand Down Expand Up @@ -208,8 +205,6 @@ public void runMigrations() {
statement.setInt(1, currentMigration);
statement.execute();
}
} catch (Exception ex) {
ex.printStackTrace();
}

}
Expand Down

0 comments on commit 2f6d21c

Please sign in to comment.