lastSessionDirectory = dbFolder.resolve(Integer.toString(mainState.headDir - 1));
// Head State
- try {
- state = HeadState.load(lastSessionDirectory);
- } catch (InvalidHeadStateException e) {
- // For backwards compatibility only!
- Throwable cause = e.getCause();
- if (cause instanceof Throwable) {
- try {
- org.simantics.db.javacore.HeadState oldState = org.simantics.db.javacore.HeadState.load(lastSessionDirectory);
-
- HeadState newState = new HeadState();
- newState.clusters = oldState.clusters;
- newState.cs = oldState.cs;
- newState.files = oldState.files;
- newState.stream = oldState.stream;
- newState.headChangeSetId = oldState.headChangeSetId;
- newState.reservedIds = oldState.reservedIds;
- newState.transactionId = oldState.transactionId;
- state = newState;
- } catch (InvalidHeadStateException e1) {
- throw new IOException("Could not load HeadState due to corruption", e1);
- }
- } else {
- // This should never happen as MainState.load() checks the integrity
- // of head.state files and rolls back in cases of corruption until a
- // consistent state is found (could be case 0 - initial db state)
- // IF this does happen something is completely wrong
- throw new IOException("Could not load HeadState due to corruption", e);
- }
- }
+ if (mainState.isInitial()) {
+ state = new HeadState();
+ } else {
+ try {
+ state = HeadState.load(lastSessionDirectory);
+ } catch (InvalidHeadStateException e) {
+ // For backwards compatibility only!
+ Throwable cause = e.getCause();
+ if (cause instanceof Throwable) {
+ try {
+ org.simantics.db.javacore.HeadState oldState = org.simantics.db.javacore.HeadState.load(lastSessionDirectory);
+
+ HeadState newState = new HeadState();
+ newState.clusters = oldState.clusters;
+ newState.cs = oldState.cs;
+ newState.files = oldState.files;
+ newState.stream = oldState.stream;
+ newState.headChangeSetId = oldState.headChangeSetId;
+ newState.reservedIds = oldState.reservedIds;
+ newState.transactionId = oldState.transactionId;
+ state = newState;
+ } catch (InvalidHeadStateException e1) {
+ throw new IOException("Could not load HeadState due to corruption", e1);
+ }
+ } else {
+ // This should never happen as MainState.load() checks the integrity
+ // of head.state files and rolls back in cases of corruption until a
+ // consistent state is found (could be case 0 - initial db state)
+ // IF this does happen something is completely wrong
+ throw new IOException("Could not load HeadState due to corruption", e);
+ }
+ }
+ }
try {
workingDirectory = dbFolder.resolve(Integer.toString(mainState.headDir));
Files.createDirectories(workingDirectory);
import java.io.File;
import java.io.IOException;
-import java.nio.channels.FileChannel;
+import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.nio.file.DirectoryStream;
-import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
-import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
import java.util.Properties;
private DatabaseUserAgent userAgent;
- private FileChannel lockFileChannel;
+ private RandomAccessFile raLockFile;
private FileLock lock;
@Override
public synchronized void start() throws ProCoreException {
try {
- lockFileChannel = lockFile.getFileSystem().provider().newFileChannel(lockFile,
- EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE));
-
- lock = lockFileChannel.tryLock();
+ raLockFile = new RandomAccessFile(lockFile.toFile(), "rw");
+ lock = raLockFile.getChannel().tryLock();
if (lock == null) {
- safeLoggingClose(lockFileChannel, lockFile);
+ safeLoggingClose(raLockFile, lockFile);
throw new ProCoreException("The database in folder " + folder.toAbsolutePath() + " is already in use!");
}
-
isRunning = true;
} catch (IOException e) {
LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e);
- safeLoggingClose(lockFileChannel, lockFile);
+ safeLoggingClose(raLockFile, lockFile);
+ throw new ProCoreException("Failed to start database at " + folder.toAbsolutePath(), e);
}
}
try {
safeLoggingClose(lock, lockFile);
lock = null;
- safeLoggingClose(lockFileChannel, lockFile);
- lockFileChannel = null;
+ safeLoggingClose(raLockFile, lockFile);
+ raLockFile = null;
Files.deleteIfExists(lockFile);
isRunning = false;
} catch (IOException e) {