Unverified 提交 d9a7cf0d authored 作者: Andrei Tokar's avatar Andrei Tokar 提交者: GitHub

Merge pull request #1465 from h2database/err-handling

Error handling improvements
...@@ -168,12 +168,14 @@ public class FileStore { ...@@ -168,12 +168,14 @@ public class FileStore {
"The file is locked: {0}", fileName, e); "The file is locked: {0}", fileName, e);
} }
if (fileLock == null) { if (fileLock == null) {
try { close(); } catch (Exception ignore) {}
throw DataUtils.newIllegalStateException( throw DataUtils.newIllegalStateException(
DataUtils.ERROR_FILE_LOCKED, DataUtils.ERROR_FILE_LOCKED,
"The file is locked: {0}", fileName); "The file is locked: {0}", fileName);
} }
fileSize = file.size(); fileSize = file.size();
} catch (IOException e) { } catch (IOException e) {
try { close(); } catch (Exception ignore) {}
throw DataUtils.newIllegalStateException( throw DataUtils.newIllegalStateException(
DataUtils.ERROR_READING_FAILED, DataUtils.ERROR_READING_FAILED,
"Could not open file {0}", fileName, e); "Could not open file {0}", fileName, e);
...@@ -184,19 +186,21 @@ public class FileStore { ...@@ -184,19 +186,21 @@ public class FileStore {
* Close this store. * Close this store.
*/ */
public void close() { public void close() {
try { if(file != null) {
if (fileLock != null) { try {
fileLock.release(); if (fileLock != null) {
fileLock = null; fileLock.release();
fileLock = null;
}
file.close();
freeSpace.clear();
} catch (Exception e) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_WRITING_FAILED,
"Closing failed for file {0}", fileName, e);
} finally {
file = null;
} }
file.close();
freeSpace.clear();
} catch (Exception e) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_WRITING_FAILED,
"Closing failed for file {0}", fileName, e);
} finally {
file = null;
} }
} }
...@@ -204,12 +208,14 @@ public class FileStore { ...@@ -204,12 +208,14 @@ public class FileStore {
* Flush all changes. * Flush all changes.
*/ */
public void sync() { public void sync() {
try { if (file != null) {
file.force(true); try {
} catch (IOException e) { file.force(true);
throw DataUtils.newIllegalStateException( } catch (IOException e) {
DataUtils.ERROR_WRITING_FAILED, throw DataUtils.newIllegalStateException(
"Could not sync file {0}", fileName, e); DataUtils.ERROR_WRITING_FAILED,
"Could not sync file {0}", fileName, e);
}
} }
} }
...@@ -228,15 +234,23 @@ public class FileStore { ...@@ -228,15 +234,23 @@ public class FileStore {
* @param size the new file size * @param size the new file size
*/ */
public void truncate(long size) { public void truncate(long size) {
try { int attemptCount = 0;
writeCount.incrementAndGet(); while (true) {
file.truncate(size); try {
fileSize = Math.min(fileSize, size); writeCount.incrementAndGet();
} catch (IOException e) { file.truncate(size);
throw DataUtils.newIllegalStateException( fileSize = Math.min(fileSize, size);
DataUtils.ERROR_WRITING_FAILED, return;
"Could not truncate file {0} to size {1}", } catch (IOException e) {
fileName, size, e); if (++attemptCount == 10) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_WRITING_FAILED,
"Could not truncate file {0} to size {1}",
fileName, size, e);
}
System.gc();
Thread.yield();
}
} }
} }
......
...@@ -943,24 +943,27 @@ public class MVStore { ...@@ -943,24 +943,27 @@ public class MVStore {
closed = true; closed = true;
storeLock.lock(); storeLock.lock();
try { try {
if (fileStore != null && shrinkIfPossible) { try {
shrinkFileIfPossible(0); if (fileStore != null && shrinkIfPossible) {
} shrinkFileIfPossible(0);
// release memory early - this is important when called }
// because of out of memory // release memory early - this is important when called
if (cache != null) { // because of out of memory
cache.clear(); if (cache != null) {
} cache.clear();
if (cacheChunkRef != null) { }
cacheChunkRef.clear(); if (cacheChunkRef != null) {
} cacheChunkRef.clear();
for (MVMap<?, ?> m : new ArrayList<>(maps.values())) { }
m.close(); for (MVMap<?, ?> m : new ArrayList<>(maps.values())) {
} m.close();
chunks.clear(); }
maps.clear(); chunks.clear();
if (fileStore != null && !fileStoreIsProvided) { maps.clear();
fileStore.close(); } finally {
if (fileStore != null && !fileStoreIsProvided) {
fileStore.close();
}
} }
} finally { } finally {
storeLock.unlock(); storeLock.unlock();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论