0

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:

  • Apache jena (3.15.0) . (TDB2)
  • Gradle 6.5
  • JVM:11.0.8 (Ubuntu 11.0.8+10-post-Ubuntu-0ubuntu120.04)

1 Answers1

1

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.

RobV
  • 28,022
  • 11
  • 77
  • 119
  • Hi, Thanks for your answer. Just to let you know that I tried to stop the Intellij and also restart the computer before run the JAR file. ALso change the database location for JAR file. Still I am facing the problem. –  Sep 10 '20 at 10:43
  • @MD.SHAHRIARHASSAN Added some more thoughts, ultimately though this is going to be down to you to investigate who/what is holding the lock on that location and resolve it accordingly – RobV Sep 10 '20 at 15:08
  • I get the main idea how it works after reading your answer. I debug the application and I saw that there are multiple threads which are trying to access the same database. Thank you. –  Sep 10 '20 at 15:27
  • Multiple threads from a single process is fine, but multiple processes are not – RobV Sep 10 '20 at 16:44