Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion h2/src/docsrc/html/features.html
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ <h3>Delayed Database Closing</h3>
<h3 id="do_not_close_on_exit">Don't Close a Database when the VM Exits</h3>
<p>
By default, a database is closed when the last connection is closed. However, if it is never closed,
the database is closed when the virtual machine exits normally, using a shutdown hook.
a persistent database is closed when the virtual machine exits normally, using a shutdown hook.
In some situations, the database should not be closed in this case, for example because the
database is still used at virtual machine shutdown (to store the shutdown process in the database for example).
For those cases, the automatic closing of the database can be disabled in the database URL.
Expand All @@ -560,6 +560,10 @@ <h3 id="do_not_close_on_exit">Don't Close a Database when the VM Exits</h3>
<pre>
String url = "jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE";
</pre>
<b>Warning:</b> when database closing on exit is disabled, an application <b>must</b> execute the
<a href="commands.html#shutdown">SHUTDOWN</a> command by itself in its own shutdown hook
after completion of all operations with database to avoid data loss
and should not try to establish new connections to database after that.

<h2 id="execute_sql_on_connection">Execute SQL on Connection</h2>
<p>
Expand Down
1 change: 1 addition & 0 deletions h2/src/main/org/h2/engine/ConnectionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public ConnectionInfo(String u, Properties info, String user, Object password) {
String[] commonSettings = { //
"ACCESS_MODE_DATA", "AUTO_RECONNECT", "AUTO_SERVER", "AUTO_SERVER_PORT", //
"CACHE_TYPE", //
"DB_CLOSE_ON_EXIT", //
"FILE_LOCK", //
"JMX", //
"NETWORK_TIMEOUT", //
Expand Down
37 changes: 28 additions & 9 deletions h2/src/main/org/h2/engine/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public final class Database implements DataHandler, CastDataProvider {
private int maxOperationMemory =
Constants.DEFAULT_MAX_OPERATION_MEMORY;
private SmallLRUCache<String, String[]> lobFileListCache;
private final boolean closeAtVmShutdown;
private final boolean autoServerMode;
private final int autoServerPort;
private Server server;
Expand Down Expand Up @@ -268,7 +269,12 @@ public Database(ConnectionInfo ci, String cipher) {
javaObjectSerializerName = s;
}
this.allowBuiltinAliasOverride = ci.getProperty("BUILTIN_ALIAS_OVERRIDE", false);
boolean closeAtVmShutdown = dbSettings.dbCloseOnExit;
if (autoServerMode && (readOnly || !persistent || fileLockMethod == FileLockMethod.NO
|| fileLockMethod == FileLockMethod.FS)) {
throw DbException.getUnsupportedException(
"AUTO_SERVER=TRUE && (readOnly || inMemory || FILE_LOCK=NO || FILE_LOCK=FS)");
}
closeAtVmShutdown = ci.getProperty("DB_CLOSE_ON_EXIT", persistent);
if (autoServerMode && !closeAtVmShutdown) {
throw DbException.getUnsupportedException("AUTO_SERVER=TRUE && DB_CLOSE_ON_EXIT=FALSE");
}
Expand Down Expand Up @@ -299,11 +305,6 @@ public Database(ConnectionInfo ci, String cipher) {
trace = traceSystem.getTrace(Trace.DATABASE);
trace.info("opening {0} (build {1})", databaseName, Constants.BUILD_ID);
try {
if (autoServerMode && (readOnly || !persistent || fileLockMethod == FileLockMethod.NO
|| fileLockMethod == FileLockMethod.FS)) {
throw DbException.getUnsupportedException(
"AUTO_SERVER=TRUE && (readOnly || inMemory || FILE_LOCK=NO || FILE_LOCK=FS)");
}
if (persistent) {
String lockFileName = databaseName + Constants.SUFFIX_LOCK_FILE;
if (readOnly) {
Expand Down Expand Up @@ -379,7 +380,7 @@ public Database(ConnectionInfo ci, String cipher) {
int writeDelay = ci.getProperty("WRITE_DELAY", Constants.DEFAULT_WRITE_DELAY);
setWriteDelay(writeDelay);
}
if (closeAtVmShutdown) {
if (closeAtVmShutdown || persistent) {
OnExitDatabaseCloser.register(this);
}
} catch (Throwable e) {
Expand Down Expand Up @@ -1069,7 +1070,7 @@ public synchronized void removeSession(SessionLocal session) {
if (isUserSession(session)) {
if (userSessions.isEmpty()) {
if (closeDelay == 0) {
close(false);
close();
} else if (closeDelay < 0) {
return;
} else {
Expand Down Expand Up @@ -1132,13 +1133,31 @@ private synchronized void closeAllSessionsExcept(SessionLocal except) {
}
}

/**
* Close the database.
*/
void close() {
close(false);
Comment thread
katzyn marked this conversation as resolved.
}

/**
* Invoked by shutdown hook.
*/
void onShutdown() {
if (closeAtVmShutdown) {
close(true);
} else if (persistent) {
checkpoint();
}
}

/**
* Close the database.
*
* @param fromShutdownHook true if this method is called from the shutdown
* hook
*/
void close(boolean fromShutdownHook) {
private void close(boolean fromShutdownHook) {
DbException b = backgroundException.getAndSet(null);
try {
closeImpl(fromShutdownHook);
Expand Down
7 changes: 0 additions & 7 deletions h2/src/main/org/h2/engine/DbSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ public class DbSettings extends SettingsBase {
*/
public final boolean caseInsensitiveIdentifiers = get("CASE_INSENSITIVE_IDENTIFIERS", false);

/**
* Database setting <code>DB_CLOSE_ON_EXIT</code> (default: true).
* Close the database when the virtual machine exits normally, using a
* shutdown hook.
*/
public final boolean dbCloseOnExit = get("DB_CLOSE_ON_EXIT", true);

/**
* Database setting <code>DEFAULT_CONNECTION</code> (default: false).
* Whether Java functions can use
Expand Down
2 changes: 1 addition & 1 deletion h2/src/main/org/h2/engine/DelayedDatabaseCloser.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void run() {
WeakReference<Database> ref = databaseRef;
if (ref != null && (database = ref.get()) != null) {
try {
database.close(false);
database.close();
} catch (RuntimeException e) {
// this can happen when stopping a web application,
// if loading classes is no longer allowed
Expand Down
2 changes: 1 addition & 1 deletion h2/src/main/org/h2/engine/OnExitDatabaseCloser.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static void onShutdown() {
RuntimeException root = null;
for (Database database : DATABASES.keySet()) {
try {
database.close(true);
database.onShutdown();
} catch (RuntimeException e) {
// this can happen when stopping a web application,
// if loading classes is no longer allowed
Expand Down
2 changes: 1 addition & 1 deletion h2/src/tools/org/h2/build/doc/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -854,4 +854,4 @@ allotted mismatched wise terminator guarding revolves notion piece submission re
duplicating unnested hardening sticky massacred
bck clo cur hwm materializedview udca vol connectionpooldatasource xadatasource
ampm sssssff sstzh tzs yyyysssss newsequentialid solidus openjdk furthermore ssff secons nashorn fractions
btrim underscores ffl decomposed decomposition subfield infinities retryable salted
btrim underscores ffl decomposed decomposition subfield infinities retryable salted establish