user4176329
user4176329

Reputation:

Why TDB2 "Lock already held" problem occurring from jar file?

I am developing an apache Jena application. It works fine when I run the codes from Intellij IDE but if I generate a jar file and run the jar file in terminal it doesn't work.

The error I get:

Uncaught exeption ! : org.apache.jena.dboe.base.file.AlreadyLocked: Failed to get a lock: file='/home/iam/OneDrive/Internship/current_data/myTDB/tdb.lock': Lock already held

I am using:

Upvotes: 0

Views: 217

Answers (1)

RobV
RobV

Reputation: 28636

TDB Specific Locking Issues

I would refer to the TDB FAQs as a first reference:

This exception is a result of TDBs automatic multi-JVM usage prevention, as noted in the earlier Can I share a TDB dataset between multiple applications? question a TDB database can only be safely used by a single JVM otherwise data corruption may occur. From 1.1.0 onwards TDB automatically enforces this restriction wherever possible and you will get this exception if you attempt to access a database which is being accessed from another JVM.

Basically you cannot open the same TDB database location from two processes simultaneously.

As for what appears to be happening in your specific case:

  1. You run some code from inside IntelliJ that accesses the TDB database. This creates the lock file, assuming that IntelliJ is not forking a new JVM then the lock file will be associated with the IntelliJ process itself.
    Or if IntelliJ is forking the new process then you/it aren't explicitly stopping that running process when you are done with it.
  2. You then try and run your built JAR file. This tries to open the TDB database, sees the lock file is present and points to the active IntelliJ process so refuses to access the database.

Most likely you'll need to begin with quitting and restarting IntelliJ since that process (or a child process) will be the lock holder. You'll then need to make sure that any code you are running from inside IntelliJ is spawned as a fresh process and that you stop that process whenever you want to test your built JAR separately.

Another possible approach would be to make your TDB database location configurable so you use a different location when testing in IntelliJ vs when running the JAR file. That way you avoid any possibility of the two processes competing over database locks because they'd be using separate databases.

File System Specific Locking Issues

I also notice that your database appears to be on a OneDrive location - /home/iam/OneDrive/Internship/current_data/myTDB/tdb.lock - it is also possible that there is a file system issue happening here.

  1. As OneDrive does not necessarily store all files locally the file system used to mount it onto your system may not actually support locking for that location. Although the fact that your code does run at all inside IntelliJ would seem to suggest this is not the case. You could however try using a database location that is definitely on your local drive to rule this out.
  2. That location is being implicitly locked by some other process (maybe OneDrive itself, maybe IntelliJ, or something else entirely)

For 2, which seems the more likely problem in your case, you can try finding out what is holding the lock e.g. How to list processes locking file to figure out what other process is locking it and investigate further.

Upvotes: 1

Related Questions