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.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.attribute.BasicFileAttributes;
import java.util.EnumSet;
import java.util.Properties;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
import java.util.Properties;
import org.simantics.db.Database;
import org.simantics.db.DatabaseUserAgent;
import org.simantics.db.ServiceLocator;
import org.simantics.db.Database;
import org.simantics.db.DatabaseUserAgent;
import org.simantics.db.ServiceLocator;
try (DirectoryStream<Path> folderStream = Files.newDirectoryStream(path)) {
return !folderStream.iterator().hasNext();
} catch (IOException e) {
try (DirectoryStream<Path> folderStream = Files.newDirectoryStream(path)) {
return !folderStream.iterator().hasNext();
} catch (IOException e) {
- if (!Files.exists(lockFile))
- Files.createFile(lockFile);
-
- raLockFile = new RandomAccessFile(lockFile.toFile(), "rw");
- lock = raLockFile.getChannel().tryLock();
+ lockFileChannel = lockFile.getFileSystem().provider().newFileChannel(lockFile,
+ EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE));
+
+ lock = lockFileChannel.tryLock();
- lock.release();
- raLockFile.close();
-
- Files.deleteIfExists(folder.resolve("lock"));
-
+ safeLoggingClose(lock, lockFile);
+ lock = null;
+ safeLoggingClose(lockFileChannel, lockFile);
+ lockFileChannel = null;
+ Files.deleteIfExists(lockFile);
- return new GraphClientImpl2(this, folder, locator);
+ if(currentClient != null) throw new DatabaseStartException(folder.toFile(), "A session is already running. Only one session is supported.");
+ currentClient = new GraphClientImpl2(this, folder, locator);
+ return currentClient;
+ private static void safeLoggingClose(AutoCloseable closeable, Path file) {
+ if (closeable == null)
+ return;
+ try (AutoCloseable c = closeable) {
+ } catch (Exception e) {
+ LOGGER.error("Failed to close " + closeable.getClass() + " of " + file.toAbsolutePath());
+ }
+ }
+