提交 63ca7dd6 authored 作者: Thomas Mueller's avatar Thomas Mueller

Trying to open a database in read-only mode when a .lock.db file exists will now…

Trying to open a database in read-only mode when a .lock.db file exists will now fail with a nice error message.
上级 9ce3143c
......@@ -18,7 +18,9 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>H2 Console: data that is too long is now abbreviated as follows: text... (100000 characters).
<ul><li>Trying to open a database in read-only mode when a .lock.db file exists will now fail
with a nice error message.
</li><li>H2 Console: data that is too long is now abbreviated as follows: text... (100000 characters).
A large binary is abbreviated as follows: hex... (100000 bytes).
</li><li>Faster data conversion from BIGINT or INT to DECIMAL.
</li></ul>
......
......@@ -601,8 +601,14 @@ public class Database implements DataHandler {
" || fileLockMethod == SERIALIZED)");
}
}
String lockFileName = databaseName + Constants.SUFFIX_LOCK_FILE;
if (readOnly) {
if (FileUtils.exists(lockFileName)) {
throw Message.getSQLException(ErrorCode.DATABASE_ALREADY_OPEN_1, "Lock file exists: " + lockFileName);
}
}
if (!readOnly && fileLockMethod != FileLock.LOCK_NO) {
lock = new FileLock(traceSystem, databaseName + Constants.SUFFIX_LOCK_FILE, Constants.LOCK_SLEEP);
lock = new FileLock(traceSystem, lockFileName, Constants.LOCK_SLEEP);
lock.lock(fileLockMethod);
if (autoServerMode) {
startServer(lock.getUniqueId());
......
......@@ -300,13 +300,6 @@ java org.h2.test.TestAll timer
readonly database: throw exception if lock file exists
console: blob: write 'binary' and '(... bytes)'
504b0304000000... (binary, 3015712 bytes)
DROP TABLE IF EXISTS TEST;
CREATE TABLE TEST(ID INT PRIMARY KEY, data blob);
INSERT INTO TEST VALUES(1, SECURE_RAND(100000));
SELECT * FROM TEST ORDER BY ID;
outer join bug
// System.setProperty("h2.pageSize", "64");
......
......@@ -14,6 +14,7 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import org.h2.constant.ErrorCode;
import org.h2.engine.Constants;
import org.h2.store.FileLister;
import org.h2.test.TestBase;
......@@ -36,6 +37,7 @@ public class TestReadOnly extends TestBase {
if (config.memory) {
return;
}
testReadOnlyConnect();
testReadOnlyDbCreate();
if (!config.googleAppEngine) {
testReadOnlyFiles(true);
......@@ -74,6 +76,7 @@ public class TestReadOnly extends TestBase {
}
private void testReadOnlyFiles(boolean setReadOnly) throws Exception {
new File(System.getProperty("java.io.tmpdir")).mkdirs();
File f = File.createTempFile("test", "temp");
assertTrue(f.canWrite());
f.setReadOnly();
......@@ -154,4 +157,20 @@ public class TestReadOnly extends TestBase {
}
}
private void testReadOnlyConnect() throws SQLException {
deleteDb("readonly");
Connection conn = getConnection("readonly;OPEN_NEW=TRUE");
Statement stat = conn.createStatement();
stat.execute("create table test(id identity)");
stat.execute("insert into test select x from system_range(1, 11)");
try {
getConnection("readonly;ACCESS_MODE_LOG=r;ACCESS_MODE_DATA=r;OPEN_NEW=TRUE");
} catch (SQLException e) {
assertEquals(ErrorCode.DATABASE_ALREADY_OPEN_1, e.getErrorCode());
}
conn.close();
deleteDb("readonly");
}
}
......@@ -33,8 +33,6 @@ public class TestFileLockSerialized extends TestBase {
}
public void test() throws Exception {
println("testReadOnly");
testReadOnlyConnect();
println("testWrongDatabaseInstanceOnReconnect");
testWrongDatabaseInstanceOnReconnect();
println("testCache()");
......@@ -63,28 +61,6 @@ public class TestFileLockSerialized extends TestBase {
testConcurrentReadWrite();
}
private void testReadOnlyConnect() throws SQLException {
deleteDb("fileLockSerialized");
String url = "jdbc:h2:" + baseDir + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
Connection conn = DriverManager.getConnection(url);
Statement stat = conn.createStatement();
stat.execute("create table test(id identity)");
stat.execute("insert into test select x from system_range(1, 10)");
Connection connReadOnly = DriverManager.getConnection(url + ";ACCESS_MODE_LOG=r;ACCESS_MODE_DATA=r");
Statement statReadOnly = connReadOnly.createStatement();
assertResult("10", statReadOnly, "select count(*) from test");
stat.execute("insert into test select x from system_range(11, 20)");
assertResult("20", statReadOnly, "select count(*) from test");
connReadOnly.close();
conn.close();
deleteDb("fileLockSerialized");
}
private void testThreeMostlyReaders(final boolean write) throws Exception {
boolean longRun = false;
deleteDb("fileLockSerialized");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论