Skip to content

Commit

Permalink
Add DatabaseManager closure on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
vLuckyyy committed Aug 18, 2024
1 parent c101597 commit bc7cac9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/com/eternalcode/discordapp/DiscordApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class DiscordApp {
private static ExperienceService experienceService;
private static LevelService levelService;
private static GitHubReviewService gitHubReviewService;
private static DatabaseManager databaseManager;

public static void main(String... args) throws InterruptedException {
Runtime.getRuntime().addShutdownHook(new Thread(DiscordApp::shutdown));
Expand All @@ -90,7 +91,7 @@ public static void main(String... args) throws InterruptedException {
}

try {
DatabaseManager databaseManager = new DatabaseManager(databaseConfig, new File("database"));
databaseManager = new DatabaseManager(databaseConfig, new File("database"));
databaseManager.connect();
UserRepositoryImpl.create(databaseManager);
GitHubReviewMentionRepository gitHubReviewMentionRepository =
Expand Down Expand Up @@ -200,6 +201,13 @@ public static void main(String... args) throws InterruptedException {
}

private static void shutdown() {
try {
databaseManager.close();
}
catch (Exception exception) {
throw new RuntimeException(exception);
}

try {
LOGGER.info("Shutting down executor service...");
EXECUTOR_SERVICE.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public ConnectionSource getConnectionSource() {
return this.connectionSource;
}

public void close() throws Exception {
try {
this.connectionSource.close();
this.hikariDataSource.close();
}
catch (SQLException sqlException) {
Sentry.captureException(sqlException);
throw new DataAccessException("Failed to close connection", sqlException);
}
}

@SuppressWarnings("unchecked")
public <T, ID> Dao<T, ID> getDao(Class<T> clazz) {
Dao<?, ?> dao = this.daoCache.computeIfAbsent(clazz, key -> {
Expand Down

0 comments on commit bc7cac9

Please sign in to comment.