Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use different databases embedded in Neo4j with Java

I am trying to evaluate different datamodels in Neo4j. For that reason I have created two databases which have those different models and saved them in the same directory:

path/to/database/graphModel1
path/to/database/graphModel2

The next step is that I have created an application with java where I can select the datamodel and the query I want to evaluate (there are only a few predefined queries). Depending which model was selected I want to use the instance of the embedded database. At the moment I use the following if-clause to distinguish between them:

if (model.equals("G1")) {
    graph = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH_1);
} else if (model.equals("G2")) {
    graph = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH_2);
} 
registerShutdownHook(graph);

But using that snippet of code I got the following exception after I did the first query with model1 and want to compute the same query with model2:

Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.StoreLockerLifecycleAdapter@1f1e9b8' was successfully initialized, but failed to start. Please see attached cause exception.
at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:513)
at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:115)
at org.neo4j.kernel.InternalAbstractGraphDatabase.run(InternalAbstractGraphDatabase.java:330)
... 69 more
Caused by: org.neo4j.kernel.StoreLockException: Unable to obtain lock on store lock file: path/to/database/graphModel1/store_lock. Please ensure no other process is using this database, and that the directory is writable (required even for read-only access)
at org.neo4j.kernel.StoreLocker.checkLock(StoreLocker.java:78)
at org.neo4j.kernel.StoreLockerLifecycleAdapter.start(StoreLockerLifecycleAdapter.java:44)
at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:507)
... 71 more
Caused by: java.io.IOException: Couldn't lock lock file path/to/database/graphModel1/lock because another process already holds the lock.
at org.neo4j.io.fs.FileLock.getLockFileBasedFileLock(FileLock.java:126)
at org.neo4j.io.fs.FileLock.getOsSpecificFileLock(FileLock.java:70)
at org.neo4j.io.fs.DefaultFileSystemAbstraction.tryLock(DefaultFileSystemAbstraction.java:85)
at org.neo4j.kernel.StoreLocker.checkLock(StoreLocker.java:74)
... 73 more

I realized that there are already a store_lock and a lock on the first database but how does this affect starting a second embedded database? Does anyone know how to solve this problem?

Thanks in advance!

like image 795
d.r.91 Avatar asked Mar 14 '26 20:03

d.r.91


1 Answers

It is possible that your GraphDatabase instance was not properly shut down - leaving it in a locked state. I see in your code above that you are registering a shutdown hook, but for graphDB not graph. This might explain how the instance was not properly shut down.

If you know there are no other instances pointing to your Neo4j directory you can try manually deleting the graph.db/lock and graph.db/store_lock files and running your code again.

like image 96
William Lyon Avatar answered Mar 16 '26 09:03

William Lyon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!