| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.engine; |
| 7 |
|
| 8 |
import java.util.WeakHashMap; |
| 9 |
|
| 10 |
import org.h2.message.Trace; |
| 11 |
|
| 12 |
/** |
| 13 |
* This class is responsible to close a database on JVM shutdown. |
| 14 |
*/ |
| 15 |
class OnExitDatabaseCloser extends Thread { |
| 16 |
|
| 17 |
private static final WeakHashMap<Database, Void> DATABASES = new WeakHashMap<>(); |
| 18 |
|
| 19 |
private static final Thread INSTANCE = new OnExitDatabaseCloser(); |
| 20 |
|
| 21 |
private static boolean registered; |
| 22 |
|
| 23 |
private static boolean terminated; |
| 24 |
|
| 25 |
/** |
| 26 |
* Register database instance to close one on the JVM process shutdown. |
| 27 |
* |
| 28 |
* @param db Database instance. |
| 29 |
*/ |
| 30 |
static synchronized void register(Database db) { |
| 31 |
if (terminated) { |
| 32 |
// Shutdown in progress |
| 33 |
return; |
| 34 |
} |
| 35 |
DATABASES.put(db, null); |
| 36 |
if (!registered) { |
| 37 |
// Mark as registered unconditionally to avoid further attempts to register a |
| 38 |
// shutdown hook in case of exception. |
| 39 |
registered = true; |
| 40 |
try { |
| 41 |
Runtime.getRuntime().addShutdownHook(INSTANCE); |
| 42 |
} catch (IllegalStateException e) { |
| 43 |
// shutdown in progress - just don't register the handler |
| 44 |
// (maybe an application wants to write something into a |
| 45 |
// database at shutdown time) |
| 46 |
} catch (SecurityException e) { |
| 47 |
// applets may not do that - ignore |
| 48 |
// Google App Engine doesn't allow |
| 49 |
// to instantiate classes that extend Thread |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Unregister database instance. |
| 56 |
* |
| 57 |
* @param db Database instance. |
| 58 |
*/ |
| 59 |
static synchronized void unregister(Database db) { |
| 60 |
if (terminated) { |
| 61 |
// Shutdown in progress, do nothing |
| 62 |
// This method can be called from the onShutdown() |
| 63 |
return; |
| 64 |
} |
| 65 |
DATABASES.remove(db); |
| 66 |
if (DATABASES.isEmpty() && registered) { |
| 67 |
try { |
| 68 |
Runtime.getRuntime().removeShutdownHook(INSTANCE); |
| 69 |
} catch (IllegalStateException e) { |
| 70 |
// ignore |
| 71 |
} catch (SecurityException e) { |
| 72 |
// applets may not do that - ignore |
| 73 |
} |
| 74 |
registered = false; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
private static void onShutdown() { |
| 79 |
synchronized(OnExitDatabaseCloser.class) { |
| 80 |
terminated = true; |
| 81 |
} |
| 82 |
RuntimeException root = null; |
| 83 |
for (Database database : DATABASES.keySet()) { |
| 84 |
try { |
| 85 |
database.close(true); |
| 86 |
} catch (RuntimeException e) { |
| 87 |
// this can happen when stopping a web application, |
| 88 |
// if loading classes is no longer allowed |
| 89 |
// it would throw an IllegalStateException |
| 90 |
try { |
| 91 |
database.getTrace(Trace.DATABASE).error(e, "could not close the database"); |
| 92 |
// if this was successful, we ignore the exception |
| 93 |
// otherwise not |
| 94 |
} catch (Throwable e2) { |
| 95 |
e.addSuppressed(e2); |
| 96 |
if (root == null) { |
| 97 |
root = e; |
| 98 |
} else { |
| 99 |
root.addSuppressed(e); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
if (root != null) { |
| 105 |
throw root; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
private OnExitDatabaseCloser() { |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void run() { |
| 114 |
onShutdown(); |
| 115 |
} |
| 116 |
|
| 117 |
} |