diff --git a/SHiNE-server/shine-server-db/src/main/java/shine/db/DbController.java b/SHiNE-server/shine-server-db/src/main/java/shine/db/DbController.java index 72dec60c..96e1c6dd 100644 --- a/SHiNE-server/shine-server-db/src/main/java/shine/db/DbController.java +++ b/SHiNE-server/shine-server-db/src/main/java/shine/db/DbController.java @@ -1,6 +1,7 @@ package shine.db; import shine.db.connection.DbProvider; +import shine.db.dao.DmDialogStateDAO; import java.sql.Connection; import java.sql.SQLException; @@ -15,6 +16,7 @@ public final class DbController implements DbProvider { private static volatile DbController instance; private final PostgresDbController delegate; + private volatile boolean dmDialogStateBootstrapped; private DbController() { this.delegate = PostgresDbController.getInstance(); @@ -24,7 +26,9 @@ public final class DbController implements DbProvider { if (instance == null) { synchronized (DbController.class) { if (instance == null) { - instance = new DbController(); + DbController created = new DbController(); + instance = created; + created.bootstrapDmDialogStateIfNeeded(); } } } @@ -40,4 +44,18 @@ public final class DbController implements DbProvider { public void close() { delegate.close(); } + + private void bootstrapDmDialogStateIfNeeded() { + if (dmDialogStateBootstrapped) return; + synchronized (this) { + if (dmDialogStateBootstrapped) return; + try (Connection connection = delegate.getConnection()) { + DmDialogStateDAO.getInstance().bootstrapIfEmpty(connection); + dmDialogStateBootstrapped = true; + } catch (SQLException e) { + instance = null; + throw new RuntimeException("DM dialog state bootstrap failed", e); + } + } + } } diff --git a/SHiNE-server/shine-server-db/src/main/java/shine/db/PostgresDbController.java b/SHiNE-server/shine-server-db/src/main/java/shine/db/PostgresDbController.java index 680af9ad..a3b77fcc 100644 --- a/SHiNE-server/shine-server-db/src/main/java/shine/db/PostgresDbController.java +++ b/SHiNE-server/shine-server-db/src/main/java/shine/db/PostgresDbController.java @@ -1,7 +1,6 @@ package shine.db; import shine.db.connection.DriverManagerDbProvider; -import shine.db.dao.DmDialogStateDAO; import utils.config.AppConfig; import java.sql.Connection; @@ -35,12 +34,6 @@ public final class PostgresDbController { this.delegate = new DriverManagerDbProvider(jdbcUrl, dbUser, dbPassword, connection -> { connection.setAutoCommit(true); }); - - try (Connection connection = this.delegate.getConnection()) { - DmDialogStateDAO.getInstance().bootstrapIfEmpty(connection); - } catch (SQLException e) { - throw new RuntimeException("DM dialog state bootstrap failed", e); - } } public static PostgresDbController getInstance() {