提交 052542ec authored 作者: Thomas Mueller's avatar Thomas Mueller

The wrong error message was thrown when trying to open a database where the…

The wrong error message was thrown when trying to open a database where the database file could not be read for some reason (for example because the split file system was used, and the file was split at the wrong position). 
上级 9ca070f2
......@@ -18,7 +18,10 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>There was a memory leak in the trace system. Opening and closing many connections could run out of memory.
<ul><li>The wrong error message was thrown when trying to open a database where the database file
could not be read for some reason (for example because the split file system was used, and the file
was split at the wrong position).
</li><li>There was a memory leak in the trace system. Opening and closing many connections could run out of memory.
</li><li>The scan-resistant cache type "TQ" (two queue) is again available.
To use it, append ;CACHE_TYPE=TQ to the database URL.
</li></ul>
......
......@@ -313,10 +313,15 @@ public class PageStore implements CacheWriter {
try {
file = database.openFile(fileName, accessMode, true);
} catch (DbException e) {
// in Windows, you can't open a locked file
// (in other operating systems, you can)
if (e.getErrorCode() == ErrorCode.IO_EXCEPTION_2) {
throw DbException.get(ErrorCode.DATABASE_ALREADY_OPEN_1, e, fileName);
if (e.getMessage().indexOf("locked") >= 0) {
// in Windows, you can't open a locked file
// (in other operating systems, you can)
// the exact error message is:
// "The process cannot access the file because
// another process has locked a portion of the file"
throw DbException.get(ErrorCode.DATABASE_ALREADY_OPEN_1, e, fileName);
}
}
throw e;
}
......
......@@ -12,7 +12,6 @@ import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.message.DbException;
import org.h2.util.New;
......@@ -240,14 +239,14 @@ public class FileSystemSplit extends FileSystem {
long l = o.length();
length += l;
if (l != maxLength) {
throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "Expected file length: " + maxLength + " got: " + l + " for " + o.getName());
throw new IOException("Expected file length: " + maxLength + " got: " + l + " for " + o.getName());
}
}
o = array[array.length - 1];
long l = o.length();
length += l;
if (l > maxLength) {
throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "Expected file length: " + maxLength + " got: " + l + " for " + o.getName());
throw new IOException("Expected file length: " + maxLength + " got: " + l + " for " + o.getName());
}
}
FileObjectSplit fo = new FileObjectSplit(fileName, mode, array, length, maxLength);
......
......@@ -46,7 +46,9 @@ public class TestLob extends TestBase {
*/
public static void main(String... a) throws Exception {
System.setProperty("h2.lobInDatabase", "true");
TestBase.createCaller().init().test();
TestBase test = TestBase.createCaller().init();
test.config.big = true;
test.test();
}
public void test() throws Exception {
......
......@@ -14,6 +14,9 @@ import java.sql.SQLException;
import java.sql.Statement;
import org.h2.api.DatabaseEventListener;
import org.h2.constant.ErrorCode;
import org.h2.store.fs.FileObject;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.tools.Restore;
import org.h2.util.Task;
......@@ -36,6 +39,8 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
}
public void test() throws Exception {
testErrorMessageLocked();
testErrorMessageWrongSplit();
testCloseDelay();
testBackup();
testCase();
......@@ -43,6 +48,39 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
deleteDb("openClose");
}
private void testErrorMessageLocked() throws Exception {
if (config.memory) {
return;
}
deleteDb("openClose");
Connection conn;
conn = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/openClose;FILE_LOCK=FS");
try {
DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/openClose;FILE_LOCK=FS;OPEN_NEW=TRUE");
} catch (SQLException e) {
assertEquals(ErrorCode.DATABASE_ALREADY_OPEN_1, e.getErrorCode());
}
conn.close();
}
private void testErrorMessageWrongSplit() throws Exception {
if (config.memory) {
return;
}
deleteDb("split:" + getBaseDir(), "openClose");
deleteDb("openClose");
Connection conn;
conn = DriverManager.getConnection("jdbc:h2:split:12:" + getBaseDir() + "/openClose");
conn.close();
FileObject f = FileSystem.getInstance(getBaseDir()).openFileObject(getBaseDir() + "/openClose.h2.db.1.part", "rw");
f.setFileLength(f.length() * 2);
try {
DriverManager.getConnection("jdbc:h2:split:12:" + getBaseDir() + "/openClose");
} catch (SQLException e) {
assertEquals(ErrorCode.IO_EXCEPTION_2, e.getErrorCode());
}
}
private void testCloseDelay() throws Exception {
deleteDb("openClose");
String url = getURL("openClose;DB_CLOSE_DELAY=1", true);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论