Skip to content

Commit 389bf48

Browse files
authored
Merge pull request #3636 from ebean-orm/feature/explicit-shutdownHook
Add DatabaseBuilder.shutdownHook option (programmatic disabling registration of JVM shutdown hook)
2 parents 3b3b445 + 297f334 commit 389bf48

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

ebean-api/src/main/java/io/ebean/DatabaseBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,11 @@ default DatabaseBuilder skipDataSourceCheck(boolean skipDataSourceCheck) {
10621062
*/
10631063
DatabaseBuilder readOnlyDatabase(boolean readOnlyDatabase);
10641064

1065+
/**
1066+
* Set to false such that the instance does not register a JVM shutdown hook.
1067+
*/
1068+
DatabaseBuilder shutdownHook(boolean shutdownHook);
1069+
10651070
/**
10661071
* Set a DataSource.
10671072
*/
@@ -2643,6 +2648,11 @@ interface Settings extends DatabaseBuilder {
26432648
*/
26442649
boolean readOnlyDatabase();
26452650

2651+
/**
2652+
* Return if a JVM shutdown hook should be registered.
2653+
*/
2654+
boolean shutdownHook();
2655+
26462656
/**
26472657
* Return the DataSource.
26482658
*/

ebean-api/src/main/java/io/ebean/config/DatabaseConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ public class DatabaseConfig implements DatabaseBuilder.Settings {
311311
private boolean skipDataSourceCheck;
312312

313313
private boolean readOnlyDatabase;
314+
private boolean shutdownHook = true;
314315

315316
/**
316317
* The data source (if programmatically provided).
@@ -1365,6 +1366,17 @@ public boolean readOnlyDatabase() {
13651366
return readOnlyDatabase;
13661367
}
13671368

1369+
@Override
1370+
public DatabaseBuilder shutdownHook(boolean shutdownHook) {
1371+
this.shutdownHook = shutdownHook;
1372+
return this;
1373+
}
1374+
1375+
@Override
1376+
public boolean shutdownHook() {
1377+
return shutdownHook;
1378+
}
1379+
13681380
@Override
13691381
public DataSource getDataSource() {
13701382
return dataSource;
@@ -2137,6 +2149,7 @@ protected void loadSettings(PropertiesWrapper p) {
21372149
loadDocStoreSettings(p);
21382150

21392151
defaultServer = p.getBoolean("defaultServer", defaultServer);
2152+
shutdownHook = p.getBoolean("shutdownHook", shutdownHook);
21402153
readOnlyDatabase = p.getBoolean("readOnlyDatabase", readOnlyDatabase);
21412154
autoPersistUpdates = p.getBoolean("autoPersistUpdates", autoPersistUpdates);
21422155
loadModuleInfo = p.getBoolean("loadModuleInfo", loadModuleInfo);

ebean-api/src/main/java/io/ebean/event/ShutdownManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ public static void deregisterShutdownHook() {
9797
private static void registerShutdownHook() {
9898
lock.lock();
9999
try {
100-
String value = System.getProperty("ebean.registerShutdownHook");
101-
if (value == null || !value.trim().equalsIgnoreCase("false")) {
100+
if ("true".equalsIgnoreCase(System.getProperty("ebean.registerShutdownHook", "true"))) {
102101
Runtime.getRuntime().addShutdownHook(shutdownHook);
103102
}
104103
} catch (IllegalStateException ex) {

ebean-api/src/test/java/io/ebean/config/DatabaseConfigTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ void testLoadWithProperties() {
7979
props.setProperty("includeLabelInSql", "false");
8080
props.setProperty("lazyLoadBatchSize", "50");
8181
props.setProperty("queryBatchSize", "60");
82+
props.setProperty("shutdownHook", "false");
8283

8384
props.setProperty("queryPlan.enable", "true");
8485
props.setProperty("queryPlan.thresholdMicros", "10000");
@@ -100,6 +101,7 @@ void testLoadWithProperties() {
100101
assertTrue(settings.isLoadModuleInfo());
101102
assertTrue(settings.skipDataSourceCheck());
102103
assertTrue(settings.readOnlyDatabase());
104+
assertFalse(settings.shutdownHook());
103105
assertFalse(settings.isIncludeLabelInSql());
104106
assertThat(settings.getLengthCheck()).isEqualTo(LengthCheck.ON);
105107
assertThat(settings.getLazyLoadBatchSize()).isEqualTo(50);
@@ -172,6 +174,7 @@ void test_defaults() {
172174
DatabaseBuilder.Settings config = new DatabaseConfig().settings();
173175
assertTrue(config.isIdGeneratorAutomatic());
174176
assertTrue(config.isDefaultServer());
177+
assertTrue(config.shutdownHook());
175178
assertFalse(config.isAutoPersistUpdates());
176179
assertFalse(config.skipDataSourceCheck());
177180

@@ -196,6 +199,8 @@ void test_defaults() {
196199
assertThat(config.getLengthCheck()).isEqualTo(LengthCheck.OFF);
197200
assertTrue(config.isIncludeLabelInSql());
198201

202+
config.shutdownHook(false);
203+
assertFalse(config.shutdownHook());
199204
config.setLoadModuleInfo(false);
200205
assertFalse(config.isAutoLoadModuleInfo());
201206
assertFalse(config.isLoadModuleInfo());

ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ public DefaultServer(InternalConfiguration config, ServerCacheManager cache) {
162162
this.scriptRunner = new DScriptRunner(this);
163163

164164
configureServerPlugins();
165-
// Register with the JVM Shutdown hook
166-
ShutdownManager.registerDatabase(this);
165+
if (this.config.shutdownHook() && "true".equalsIgnoreCase(System.getProperty("ebean.registerShutdownHook", "true"))) {
166+
// register with the JVM Shutdown hook
167+
ShutdownManager.registerDatabase(this);
168+
}
167169
}
168170

169171
/**

0 commit comments

Comments
 (0)