From 6e2857d98662cf85870f3ba3fb4838645d625288 Mon Sep 17 00:00:00 2001 From: Antoine LCDP Date: Tue, 9 Jul 2024 15:07:20 +0200 Subject: [PATCH] Create EbeanLifecycle component to manage Ebean shutdown --- .../java/play/db/ebean/EBeanComponents.java | 7 +++-- .../play/db/ebean/EbeanDynamicEvolutions.java | 19 +++++++++----- .../java/play/db/ebean/EbeanLifecycle.java | 26 +++++++++++++++++++ .../main/java/play/db/ebean/EbeanModule.java | 3 ++- 4 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 play-ebean/src/main/java/play/db/ebean/EbeanLifecycle.java diff --git a/play-ebean/src/main/java/play/db/ebean/EBeanComponents.java b/play-ebean/src/main/java/play/db/ebean/EBeanComponents.java index da61057c..604953a5 100644 --- a/play-ebean/src/main/java/play/db/ebean/EBeanComponents.java +++ b/play-ebean/src/main/java/play/db/ebean/EBeanComponents.java @@ -14,14 +14,17 @@ public interface EBeanComponents extends ConfigurationComponents, DBComponents { default DynamicEvolutions dynamicEvolutions() { - return new EbeanDynamicEvolutions( - ebeanConfig(), environment(), applicationLifecycle(), evolutionsConfig()); + return new EbeanDynamicEvolutions(ebeanConfig(), environment(), evolutionsConfig()); } default EbeanConfig ebeanConfig() { return new DefaultEbeanConfig.EbeanConfigParser(config(), environment(), dbApi()).get(); } + default EbeanLifecycle ebeanLifecycle() { + return new EbeanLifecycle(applicationLifecycle()); + } + default EvolutionsConfig evolutionsConfig() { return new DefaultEvolutionsConfigParser(configuration()).parse(); } diff --git a/play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java b/play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java index dcb47ccb..73e4c0cd 100644 --- a/play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java +++ b/play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java @@ -14,7 +14,6 @@ import java.nio.file.Files; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.CompletableFuture; import javax.inject.Inject; import javax.inject.Singleton; import play.Environment; @@ -34,21 +33,27 @@ public class EbeanDynamicEvolutions extends DynamicEvolutions { private final Map databases = new HashMap<>(); - @Inject + /** + * @deprecated No need to pass lifecycle anymore as shutdown is now managed by {@link + * play.db.ebean.EbeanLifecycle}. Use {@link #EbeanDynamicEvolutions(EbeanConfig, Environment, + * EvolutionsConfig)} instead. + */ + @Deprecated public EbeanDynamicEvolutions( EbeanConfig config, Environment environment, ApplicationLifecycle lifecycle, EvolutionsConfig evolutionsConfig) { + this(config, environment, evolutionsConfig); + } + + @Inject + public EbeanDynamicEvolutions( + EbeanConfig config, Environment environment, EvolutionsConfig evolutionsConfig) { this.config = config; this.environment = environment; this.evolutionsConfig = evolutionsConfig; start(); - lifecycle.addStopHook( - () -> { - databases.forEach((key, database) -> database.shutdown(false, false)); - return CompletableFuture.completedFuture(null); - }); } /** Initialise the Ebean servers/databases. */ diff --git a/play-ebean/src/main/java/play/db/ebean/EbeanLifecycle.java b/play-ebean/src/main/java/play/db/ebean/EbeanLifecycle.java new file mode 100644 index 00000000..1bb6d092 --- /dev/null +++ b/play-ebean/src/main/java/play/db/ebean/EbeanLifecycle.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. + */ + +package play.db.ebean; + +import io.ebean.event.ShutdownManager; +import java.util.concurrent.CompletableFuture; +import javax.inject.Inject; +import javax.inject.Singleton; +import play.api.db.evolutions.DynamicEvolutions; +import play.inject.ApplicationLifecycle; + +/** A Play module that automatically manages Ebean configuration. */ +@Singleton +public class EbeanLifecycle extends DynamicEvolutions { + @Inject + public EbeanLifecycle(ApplicationLifecycle lifecycle) { + ShutdownManager.deregisterShutdownHook(); + lifecycle.addStopHook( + () -> { + ShutdownManager.shutdown(); + return CompletableFuture.completedFuture(null); + }); + } +} diff --git a/play-ebean/src/main/java/play/db/ebean/EbeanModule.java b/play-ebean/src/main/java/play/db/ebean/EbeanModule.java index f83f74d1..6739a12e 100644 --- a/play-ebean/src/main/java/play/db/ebean/EbeanModule.java +++ b/play-ebean/src/main/java/play/db/ebean/EbeanModule.java @@ -18,6 +18,7 @@ public class EbeanModule extends Module { public Seq> bindings(Environment environment, Configuration configuration) { return seq( bind(DynamicEvolutions.class).to(EbeanDynamicEvolutions.class).eagerly(), - bind(EbeanConfig.class).toProvider(DefaultEbeanConfig.EbeanConfigParser.class).eagerly()); + bind(EbeanConfig.class).toProvider(DefaultEbeanConfig.EbeanConfigParser.class).eagerly(), + bind(EbeanLifecycle.class).toSelf().eagerly()); } }