提交 13ab7e1e authored 作者: andrei's avatar andrei

ensure Engine.close() is called

上级 cebf433e
...@@ -1256,6 +1256,7 @@ public class Database implements DataHandler { ...@@ -1256,6 +1256,7 @@ public class Database implements DataHandler {
* hook * hook
*/ */
void close(boolean fromShutdownHook) { void close(boolean fromShutdownHook) {
try {
synchronized (this) { synchronized (this) {
if (closing) { if (closing) {
return; return;
...@@ -1270,7 +1271,6 @@ public class Database implements DataHandler { ...@@ -1270,7 +1271,6 @@ public class Database implements DataHandler {
// ignore // ignore
} }
traceSystem.close(); traceSystem.close();
Engine.getInstance().close(databaseName);
return; return;
} }
closing = true; closing = true;
...@@ -1350,7 +1350,6 @@ public class Database implements DataHandler { ...@@ -1350,7 +1350,6 @@ public class Database implements DataHandler {
} }
closeOnExit = null; closeOnExit = null;
} }
Engine.getInstance().close(databaseName);
if (deleteFilesOnDisconnect && persistent) { if (deleteFilesOnDisconnect && persistent) {
deleteFilesOnDisconnect = false; deleteFilesOnDisconnect = false;
try { try {
...@@ -1361,6 +1360,9 @@ public class Database implements DataHandler { ...@@ -1361,6 +1360,9 @@ public class Database implements DataHandler {
// ignore (the trace is closed already) // ignore (the trace is closed already)
} }
} }
} finally {
Engine.getInstance().close(databaseName);
}
} }
private void removeOrphanedLobs() { private void removeOrphanedLobs() {
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
package org.h2.engine; package org.h2.engine;
import java.util.Hashtable; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
...@@ -28,7 +28,7 @@ import org.h2.util.Utils; ...@@ -28,7 +28,7 @@ import org.h2.util.Utils;
public class Engine implements SessionFactory { public class Engine implements SessionFactory {
private static final Engine INSTANCE = new Engine(); private static final Engine INSTANCE = new Engine();
private static final Map<String, Database> DATABASES = new Hashtable<>(); private static final Map<String, Database> DATABASES = new HashMap<>();
private volatile long wrongPasswordDelay = private volatile long wrongPasswordDelay =
SysProperties.DELAY_WRONG_PASSWORD_MIN; SysProperties.DELAY_WRONG_PASSWORD_MIN;
...@@ -51,13 +51,14 @@ public class Engine implements SessionFactory { ...@@ -51,13 +51,14 @@ public class Engine implements SessionFactory {
Database database; Database database;
ci.removeProperty("NO_UPGRADE", false); ci.removeProperty("NO_UPGRADE", false);
boolean openNew = ci.getProperty("OPEN_NEW", false); boolean openNew = ci.getProperty("OPEN_NEW", false);
boolean opened = false;
User user = null;
synchronized (DATABASES) {
if (openNew || ci.isUnnamedInMemory()) { if (openNew || ci.isUnnamedInMemory()) {
database = null; database = null;
} else { } else {
database = DATABASES.get(name); database = DATABASES.get(name);
} }
User user = null;
boolean opened = false;
if (database == null) { if (database == null) {
if (ifExists && !Database.exists(name)) { if (ifExists && !Database.exists(name)) {
throw DbException.get(ErrorCode.DATABASE_NOT_FOUND_1, name); throw DbException.get(ErrorCode.DATABASE_NOT_FOUND_1, name);
...@@ -77,6 +78,7 @@ public class Engine implements SessionFactory { ...@@ -77,6 +78,7 @@ public class Engine implements SessionFactory {
DATABASES.put(name, database); DATABASES.put(name, database);
} }
} }
}
if (opened) { if (opened) {
// start the thread when already synchronizing on the database // start the thread when already synchronizing on the database
// otherwise a deadlock can occur when the writer thread // otherwise a deadlock can occur when the writer thread
...@@ -273,8 +275,10 @@ public class Engine implements SessionFactory { ...@@ -273,8 +275,10 @@ public class Engine implements SessionFactory {
throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, e, "JMX"); throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, e, "JMX");
} }
} }
synchronized (DATABASES) {
DATABASES.remove(name); DATABASES.remove(name);
} }
}
/** /**
* This method is called after validating user name and password. If user * This method is called after validating user name and password. If user
......
...@@ -118,10 +118,7 @@ public class TestOutOfMemory extends TestBase { ...@@ -118,10 +118,7 @@ public class TestOutOfMemory extends TestBase {
ErrorCode.DATABASE_IS_CLOSED == e.getErrorCode() || ErrorCode.DATABASE_IS_CLOSED == e.getErrorCode() ||
ErrorCode.GENERAL_ERROR_1 == e.getErrorCode()); ErrorCode.GENERAL_ERROR_1 == e.getErrorCode());
} }
for (int i = 0; i < 5; i++) { recoverAfterOOM();
System.gc();
Thread.sleep(20);
}
try { try {
conn.close(); conn.close();
fail(); fail();
...@@ -132,10 +129,7 @@ public class TestOutOfMemory extends TestBase { ...@@ -132,10 +129,7 @@ public class TestOutOfMemory extends TestBase {
ErrorCode.DATABASE_IS_CLOSED == e.getErrorCode() || ErrorCode.DATABASE_IS_CLOSED == e.getErrorCode() ||
ErrorCode.GENERAL_ERROR_1 == e.getErrorCode()); ErrorCode.GENERAL_ERROR_1 == e.getErrorCode());
} }
for (int i = 0; i < 5; i++) { recoverAfterOOM();
System.gc();
Thread.sleep(20);
}
conn = DriverManager.getConnection(url); conn = DriverManager.getConnection(url);
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("SELECT 1"); stat.execute("SELECT 1");
...@@ -146,14 +140,18 @@ public class TestOutOfMemory extends TestBase { ...@@ -146,14 +140,18 @@ public class TestOutOfMemory extends TestBase {
} }
} }
private void testUpdateWhenNearlyOutOfMemory() throws SQLException, InterruptedException { private static void recoverAfterOOM() throws InterruptedException {
if (config.memory) {
return;
}
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
System.gc(); System.gc();
Thread.sleep(20); Thread.sleep(20);
} }
}
private void testUpdateWhenNearlyOutOfMemory() throws SQLException, InterruptedException {
if (config.memory) {
return;
}
recoverAfterOOM();
deleteDb("outOfMemory"); deleteDb("outOfMemory");
Connection conn = getConnection("outOfMemory;MAX_OPERATION_MEMORY=1000000"); Connection conn = getConnection("outOfMemory;MAX_OPERATION_MEMORY=1000000");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
...@@ -171,11 +169,12 @@ public class TestOutOfMemory extends TestBase { ...@@ -171,11 +169,12 @@ public class TestOutOfMemory extends TestBase {
fail(); fail();
} catch(DbException ex) { } catch(DbException ex) {
freeMemory(); freeMemory();
assertEquals(ErrorCode.OUT_OF_MEMORY, ex.getErrorCode()); assertTrue(ErrorCode.OUT_OF_MEMORY == ex.getErrorCode() || ErrorCode.GENERAL_ERROR_1 == ex.getErrorCode());
} catch (SQLException ex) { } catch (SQLException ex) {
freeMemory(); freeMemory();
assertEquals(ErrorCode.OUT_OF_MEMORY, ex.getErrorCode()); assertTrue(ErrorCode.OUT_OF_MEMORY == ex.getErrorCode() || ErrorCode.GENERAL_ERROR_1 == ex.getErrorCode());
} }
recoverAfterOOM();
try { try {
conn.close(); conn.close();
fail(); fail();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论