提交 2d4f153e authored 作者: Noel Grandin's avatar Noel Grandin

use enum for file lock method

上级 469a69ea
...@@ -44,6 +44,7 @@ import org.h2.schema.Sequence; ...@@ -44,6 +44,7 @@ import org.h2.schema.Sequence;
import org.h2.schema.TriggerObject; import org.h2.schema.TriggerObject;
import org.h2.store.DataHandler; import org.h2.store.DataHandler;
import org.h2.store.FileLock; import org.h2.store.FileLock;
import org.h2.store.FileLockMethod;
import org.h2.store.FileStore; import org.h2.store.FileStore;
import org.h2.store.InDoubtTransaction; import org.h2.store.InDoubtTransaction;
import org.h2.store.LobStorageBackend; import org.h2.store.LobStorageBackend;
...@@ -139,7 +140,7 @@ public class Database implements DataHandler { ...@@ -139,7 +140,7 @@ public class Database implements DataHandler {
private boolean starting; private boolean starting;
private TraceSystem traceSystem; private TraceSystem traceSystem;
private Trace trace; private Trace trace;
private final int fileLockMethod; private final FileLockMethod fileLockMethod;
private Role publicRole; private Role publicRole;
private final AtomicLong modificationDataId = new AtomicLong(); private final AtomicLong modificationDataId = new AtomicLong();
private final AtomicLong modificationMetaId = new AtomicLong(); private final AtomicLong modificationMetaId = new AtomicLong();
...@@ -235,14 +236,14 @@ public class Database implements DataHandler { ...@@ -235,14 +236,14 @@ public class Database implements DataHandler {
} }
if (dbSettings.mvStore && lockMethodName == null) { if (dbSettings.mvStore && lockMethodName == null) {
if (autoServerMode) { if (autoServerMode) {
fileLockMethod = FileLock.LOCK_FILE; fileLockMethod = FileLockMethod.FILE;
} else { } else {
fileLockMethod = FileLock.LOCK_FS; fileLockMethod = FileLockMethod.FS;
} }
} else { } else {
fileLockMethod = FileLock.getFileLockMethod(lockMethodName); fileLockMethod = FileLock.getFileLockMethod(lockMethodName);
} }
if (dbSettings.mvStore && fileLockMethod == FileLock.LOCK_SERIALIZED) { if (dbSettings.mvStore && fileLockMethod == FileLockMethod.SERIALIZED) {
throw DbException.getUnsupportedException( throw DbException.getUnsupportedException(
"MV_STORE combined with FILE_LOCK=SERIALIZED"); "MV_STORE combined with FILE_LOCK=SERIALIZED");
} }
...@@ -407,7 +408,7 @@ public class Database implements DataHandler { ...@@ -407,7 +408,7 @@ public class Database implements DataHandler {
*/ */
private synchronized boolean reconnectModified(boolean pending) { private synchronized boolean reconnectModified(boolean pending) {
if (readOnly || lock == null || if (readOnly || lock == null ||
fileLockMethod != FileLock.LOCK_SERIALIZED) { fileLockMethod != FileLockMethod.SERIALIZED) {
return true; return true;
} }
try { try {
...@@ -516,7 +517,7 @@ public class Database implements DataHandler { ...@@ -516,7 +517,7 @@ public class Database implements DataHandler {
} }
if (lock != null) { if (lock != null) {
stopServer(); stopServer();
if (fileLockMethod != FileLock.LOCK_SERIALIZED) { if (fileLockMethod != FileLockMethod.SERIALIZED) {
// allow testing shutdown // allow testing shutdown
lock.unlock(); lock.unlock();
} }
...@@ -646,9 +647,9 @@ public class Database implements DataHandler { ...@@ -646,9 +647,9 @@ public class Database implements DataHandler {
trace.info("opening {0} (build {1})", databaseName, Constants.BUILD_ID); trace.info("opening {0} (build {1})", databaseName, Constants.BUILD_ID);
if (autoServerMode) { if (autoServerMode) {
if (readOnly || if (readOnly ||
fileLockMethod == FileLock.LOCK_NO || fileLockMethod == FileLockMethod.NO ||
fileLockMethod == FileLock.LOCK_SERIALIZED || fileLockMethod == FileLockMethod.SERIALIZED ||
fileLockMethod == FileLock.LOCK_FS || fileLockMethod == FileLockMethod.FS ||
!persistent) { !persistent) {
throw DbException.getUnsupportedException( throw DbException.getUnsupportedException(
"autoServerMode && (readOnly || " + "autoServerMode && (readOnly || " +
...@@ -665,8 +666,8 @@ public class Database implements DataHandler { ...@@ -665,8 +666,8 @@ public class Database implements DataHandler {
"Lock file exists: " + lockFileName); "Lock file exists: " + lockFileName);
} }
} }
if (!readOnly && fileLockMethod != FileLock.LOCK_NO) { if (!readOnly && fileLockMethod != FileLockMethod.NO) {
if (fileLockMethod != FileLock.LOCK_FS) { if (fileLockMethod != FileLockMethod.FS) {
lock = new FileLock(traceSystem, lockFileName, Constants.LOCK_SLEEP); lock = new FileLock(traceSystem, lockFileName, Constants.LOCK_SLEEP);
lock.lock(fileLockMethod); lock.lock(fileLockMethod);
if (autoServerMode) { if (autoServerMode) {
...@@ -1260,7 +1261,7 @@ public class Database implements DataHandler { ...@@ -1260,7 +1261,7 @@ public class Database implements DataHandler {
return; return;
} }
throwLastBackgroundException(); throwLastBackgroundException();
if (fileLockMethod == FileLock.LOCK_SERIALIZED && if (fileLockMethod == FileLockMethod.SERIALIZED &&
!reconnectChangePending) { !reconnectChangePending) {
// another connection may have written something - don't write // another connection may have written something - don't write
try { try {
...@@ -1437,8 +1438,8 @@ public class Database implements DataHandler { ...@@ -1437,8 +1438,8 @@ public class Database implements DataHandler {
} }
closeFiles(); closeFiles();
if (persistent && lock == null && if (persistent && lock == null &&
fileLockMethod != FileLock.LOCK_NO && fileLockMethod != FileLockMethod.NO &&
fileLockMethod != FileLock.LOCK_FS) { fileLockMethod != FileLockMethod.FS) {
// everything already closed (maybe in checkPowerOff) // everything already closed (maybe in checkPowerOff)
// don't delete temp files in this case because // don't delete temp files in this case because
// the database could be open now (even from within another process) // the database could be open now (even from within another process)
...@@ -1456,7 +1457,7 @@ public class Database implements DataHandler { ...@@ -1456,7 +1457,7 @@ public class Database implements DataHandler {
lobSession = null; lobSession = null;
} }
if (lock != null) { if (lock != null) {
if (fileLockMethod == FileLock.LOCK_SERIALIZED) { if (fileLockMethod == FileLockMethod.SERIALIZED) {
// wait before deleting the .lock file, // wait before deleting the .lock file,
// otherwise other connections can not detect that // otherwise other connections can not detect that
if (lock.load().containsKey("changePending")) { if (lock.load().containsKey("changePending")) {
...@@ -1985,7 +1986,7 @@ public class Database implements DataHandler { ...@@ -1985,7 +1986,7 @@ public class Database implements DataHandler {
if (readOnly) { if (readOnly) {
throw DbException.get(ErrorCode.DATABASE_IS_READ_ONLY); throw DbException.get(ErrorCode.DATABASE_IS_READ_ONLY);
} }
if (fileLockMethod == FileLock.LOCK_SERIALIZED) { if (fileLockMethod == FileLockMethod.SERIALIZED) {
if (!reconnectChangePending) { if (!reconnectChangePending) {
throw DbException.get(ErrorCode.DATABASE_IS_READ_ONLY); throw DbException.get(ErrorCode.DATABASE_IS_READ_ONLY);
} }
...@@ -2542,7 +2543,7 @@ public class Database implements DataHandler { ...@@ -2542,7 +2543,7 @@ public class Database implements DataHandler {
if (pageSize != Constants.DEFAULT_PAGE_SIZE) { if (pageSize != Constants.DEFAULT_PAGE_SIZE) {
pageStore.setPageSize(pageSize); pageStore.setPageSize(pageSize);
} }
if (!readOnly && fileLockMethod == FileLock.LOCK_FS) { if (!readOnly && fileLockMethod == FileLockMethod.FS) {
pageStore.setLockFile(true); pageStore.setLockFile(true);
} }
pageStore.setLogMode(logMode); pageStore.setLogMode(logMode);
...@@ -2578,7 +2579,7 @@ public class Database implements DataHandler { ...@@ -2578,7 +2579,7 @@ public class Database implements DataHandler {
* @return true if reconnecting is required * @return true if reconnecting is required
*/ */
public boolean isReconnectNeeded() { public boolean isReconnectNeeded() {
if (fileLockMethod != FileLock.LOCK_SERIALIZED) { if (fileLockMethod != FileLockMethod.SERIALIZED) {
return false; return false;
} }
if (reconnectChangePending) { if (reconnectChangePending) {
...@@ -2632,7 +2633,7 @@ public class Database implements DataHandler { ...@@ -2632,7 +2633,7 @@ public class Database implements DataHandler {
* the .lock.db file. * the .lock.db file.
*/ */
public void checkpointIfRequired() { public void checkpointIfRequired() {
if (fileLockMethod != FileLock.LOCK_SERIALIZED || if (fileLockMethod != FileLockMethod.SERIALIZED ||
readOnly || !reconnectChangePending || closing) { readOnly || !reconnectChangePending || closing) {
return; return;
} }
...@@ -2661,7 +2662,7 @@ public class Database implements DataHandler { ...@@ -2661,7 +2662,7 @@ public class Database implements DataHandler {
} }
public boolean isFileLockSerialized() { public boolean isFileLockSerialized() {
return fileLockMethod == FileLock.LOCK_SERIALIZED; return fileLockMethod == FileLockMethod.SERIALIZED;
} }
private void flushSequences() { private void flushSequences() {
...@@ -2695,7 +2696,7 @@ public class Database implements DataHandler { ...@@ -2695,7 +2696,7 @@ public class Database implements DataHandler {
* false if another connection was faster * false if another connection was faster
*/ */
public boolean beforeWriting() { public boolean beforeWriting() {
if (fileLockMethod != FileLock.LOCK_SERIALIZED) { if (fileLockMethod != FileLockMethod.SERIALIZED) {
return true; return true;
} }
while (checkpointRunning) { while (checkpointRunning) {
...@@ -2724,7 +2725,7 @@ public class Database implements DataHandler { ...@@ -2724,7 +2725,7 @@ public class Database implements DataHandler {
* This method is called after updates are finished. * This method is called after updates are finished.
*/ */
public void afterWriting() { public void afterWriting() {
if (fileLockMethod != FileLock.LOCK_SERIALIZED) { if (fileLockMethod != FileLockMethod.SERIALIZED) {
return; return;
} }
synchronized (reconnectSync) { synchronized (reconnectSync) {
......
...@@ -7,7 +7,6 @@ package org.h2.engine; ...@@ -7,7 +7,6 @@ package org.h2.engine;
import java.util.HashMap; import java.util.HashMap;
import java.util.Objects; import java.util.Objects;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface; import org.h2.command.CommandInterface;
import org.h2.command.Parser; import org.h2.command.Parser;
...@@ -15,6 +14,7 @@ import org.h2.command.dml.SetTypes; ...@@ -15,6 +14,7 @@ import org.h2.command.dml.SetTypes;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.message.Trace; import org.h2.message.Trace;
import org.h2.store.FileLock; import org.h2.store.FileLock;
import org.h2.store.FileLockMethod;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.ThreadDeadlockDetector; import org.h2.util.ThreadDeadlockDetector;
import org.h2.util.Utils; import org.h2.util.Utils;
...@@ -141,8 +141,8 @@ public class Engine implements SessionFactory { ...@@ -141,8 +141,8 @@ public class Engine implements SessionFactory {
try { try {
ConnectionInfo backup = null; ConnectionInfo backup = null;
String lockMethodName = ci.getProperty("FILE_LOCK", null); String lockMethodName = ci.getProperty("FILE_LOCK", null);
int fileLockMethod = FileLock.getFileLockMethod(lockMethodName); FileLockMethod fileLockMethod = FileLock.getFileLockMethod(lockMethodName);
if (fileLockMethod == FileLock.LOCK_SERIALIZED) { if (fileLockMethod == FileLockMethod.SERIALIZED) {
// In serialized mode, database instance sharing is not possible // In serialized mode, database instance sharing is not possible
ci.setProperty("OPEN_NEW", "TRUE"); ci.setProperty("OPEN_NEW", "TRUE");
try { try {
......
...@@ -41,7 +41,7 @@ public class FileLister { ...@@ -41,7 +41,7 @@ public class FileLister {
FileLock lock = new FileLock(new TraceSystem(null), fileName, FileLock lock = new FileLock(new TraceSystem(null), fileName,
Constants.LOCK_SLEEP); Constants.LOCK_SLEEP);
try { try {
lock.lock(FileLock.LOCK_FILE); lock.lock(FileLockMethod.FILE);
lock.unlock(); lock.unlock();
} catch (DbException e) { } catch (DbException e) {
throw DbException.get( throw DbException.get(
......
...@@ -35,33 +35,6 @@ import org.h2.value.Transfer; ...@@ -35,33 +35,6 @@ import org.h2.value.Transfer;
*/ */
public class FileLock implements Runnable { public class FileLock implements Runnable {
/**
* This locking method means no locking is used at all.
*/
public static final int LOCK_NO = 0;
/**
* This locking method means the cooperative file locking protocol should be
* used.
*/
public static final int LOCK_FILE = 1;
/**
* This locking method means a socket is created on the given machine.
*/
public static final int LOCK_SOCKET = 2;
/**
* This locking method means multiple writers are allowed, and they
* synchronize themselves.
*/
public static final int LOCK_SERIALIZED = 3;
/**
* Use the file system to lock the file; don't use a separate lock file.
*/
public static final int LOCK_FS = 4;
private static final String MAGIC = "FileLock"; private static final String MAGIC = "FileLock";
private static final String FILE = "file"; private static final String FILE = "file";
private static final String SOCKET = "socket"; private static final String SOCKET = "socket";
...@@ -125,22 +98,22 @@ public class FileLock implements Runnable { ...@@ -125,22 +98,22 @@ public class FileLock implements Runnable {
* @param fileLockMethod the file locking method to use * @param fileLockMethod the file locking method to use
* @throws DbException if locking was not successful * @throws DbException if locking was not successful
*/ */
public synchronized void lock(int fileLockMethod) { public synchronized void lock(FileLockMethod fileLockMethod) {
checkServer(); checkServer();
if (locked) { if (locked) {
DbException.throwInternalError("already locked"); DbException.throwInternalError("already locked");
} }
switch (fileLockMethod) { switch (fileLockMethod) {
case LOCK_FILE: case FILE:
lockFile(); lockFile();
break; break;
case LOCK_SOCKET: case SOCKET:
lockSocket(); lockSocket();
break; break;
case LOCK_SERIALIZED: case SERIALIZED:
lockSerialized(); lockSerialized();
break; break;
case LOCK_FS: case FS:
break; break;
} }
locked = true; locked = true;
...@@ -480,17 +453,17 @@ public class FileLock implements Runnable { ...@@ -480,17 +453,17 @@ public class FileLock implements Runnable {
* @return the method type * @return the method type
* @throws DbException if the method name is unknown * @throws DbException if the method name is unknown
*/ */
public static int getFileLockMethod(String method) { public static FileLockMethod getFileLockMethod(String method) {
if (method == null || method.equalsIgnoreCase("FILE")) { if (method == null || method.equalsIgnoreCase("FILE")) {
return FileLock.LOCK_FILE; return FileLockMethod.FILE;
} else if (method.equalsIgnoreCase("NO")) { } else if (method.equalsIgnoreCase("NO")) {
return FileLock.LOCK_NO; return FileLockMethod.NO;
} else if (method.equalsIgnoreCase("SOCKET")) { } else if (method.equalsIgnoreCase("SOCKET")) {
return FileLock.LOCK_SOCKET; return FileLockMethod.SOCKET;
} else if (method.equalsIgnoreCase("SERIALIZED")) { } else if (method.equalsIgnoreCase("SERIALIZED")) {
return FileLock.LOCK_SERIALIZED; return FileLockMethod.SERIALIZED;
} else if (method.equalsIgnoreCase("FS")) { } else if (method.equalsIgnoreCase("FS")) {
return FileLock.LOCK_FS; return FileLockMethod.FS;
} else { } else {
throw DbException.get( throw DbException.get(
ErrorCode.UNSUPPORTED_LOCK_METHOD_1, method); ErrorCode.UNSUPPORTED_LOCK_METHOD_1, method);
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.store;
public enum FileLockMethod {
/**
* This locking method means no locking is used at all.
*/
NO,
/**
* This locking method means the cooperative file locking protocol should be
* used.
*/
FILE,
/**
* This locking method means a socket is created on the given machine.
*/
SOCKET,
/**
* This locking method means multiple writers are allowed, and they
* synchronize themselves.
*/
SERIALIZED,
/**
* Use the file system to lock the file; don't use a separate lock file.
*/
FS;
}
...@@ -7,11 +7,11 @@ package org.h2.test.unit; ...@@ -7,11 +7,11 @@ package org.h2.test.unit;
import java.io.File; import java.io.File;
import java.sql.Connection; import java.sql.Connection;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.message.TraceSystem; import org.h2.message.TraceSystem;
import org.h2.store.FileLock; import org.h2.store.FileLock;
import org.h2.store.FileLockMethod;
import org.h2.test.TestBase; import org.h2.test.TestBase;
/** /**
...@@ -77,7 +77,7 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -77,7 +77,7 @@ public class TestFileLock extends TestBase implements Runnable {
f.setLastModified(System.currentTimeMillis() + 10000); f.setLastModified(System.currentTimeMillis() + 10000);
FileLock lock = new FileLock(new TraceSystem(null), getFile(), FileLock lock = new FileLock(new TraceSystem(null), getFile(),
Constants.LOCK_SLEEP); Constants.LOCK_SLEEP);
lock.lock(FileLock.LOCK_FILE); lock.lock(FileLockMethod.FILE);
lock.unlock(); lock.unlock();
} }
...@@ -86,14 +86,14 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -86,14 +86,14 @@ public class TestFileLock extends TestBase implements Runnable {
Constants.LOCK_SLEEP); Constants.LOCK_SLEEP);
FileLock lock2 = new FileLock(new TraceSystem(null), getFile(), FileLock lock2 = new FileLock(new TraceSystem(null), getFile(),
Constants.LOCK_SLEEP); Constants.LOCK_SLEEP);
lock1.lock(FileLock.LOCK_FILE); lock1.lock(FileLockMethod.FILE);
createClassProxy(FileLock.class); createClassProxy(FileLock.class);
assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, lock2).lock( assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, lock2).lock(
FileLock.LOCK_FILE); FileLockMethod.FILE);
lock1.unlock(); lock1.unlock();
lock2 = new FileLock(new TraceSystem(null), getFile(), lock2 = new FileLock(new TraceSystem(null), getFile(),
Constants.LOCK_SLEEP); Constants.LOCK_SLEEP);
lock2.lock(FileLock.LOCK_FILE); lock2.lock(FileLockMethod.FILE);
lock2.unlock(); lock2.unlock();
} }
...@@ -123,8 +123,8 @@ public class TestFileLock extends TestBase implements Runnable { ...@@ -123,8 +123,8 @@ public class TestFileLock extends TestBase implements Runnable {
while (!stop) { while (!stop) {
lock = new FileLock(new TraceSystem(null), getFile(), 100); lock = new FileLock(new TraceSystem(null), getFile(), 100);
try { try {
lock.lock(allowSockets ? FileLock.LOCK_SOCKET lock.lock(allowSockets ? FileLockMethod.SOCKET
: FileLock.LOCK_FILE); : FileLockMethod.FILE);
base.trace(lock + " locked"); base.trace(lock + " locked");
locks++; locks++;
if (locks > 1) { if (locks > 1) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论