Simultaneous access to the same file using JPA


I have a desktop java application that can be run by different users. It makes use of JPA to access a database that is stored as a file. For this purpose I do not want to run a separate database server.

The purpose of the database is to store actions that are done in the program, it's a simple "store record" operation. But all users must be able to read these stored records.

How can I make sure the different applications can save their actions, while not overwriting the actions of the other? So I need a way to:

  1. Open the database (file)
  2. Lock it
  3. Write the added action
  4. Close the database (file)

Is there a way in JPA to do/enforce this ? I'm now using hibernate, but that's not a strict requirement.

Please do not answer with "you can do this with technology XXX". Please note that I'm concerned with respect to the concurrency issues and how to enforce that the file is opened, and closed again. How can this be done with technology XXX ?

You can try to use SQLite database file. In this case, you can achieve concurrent access to the same file (e.g. database), and in the same time you can use SQLite JDBC driver along with your JPA provider (e.g. Hibernate).

The only disadvantage might be is that, strictly saying, it's not pure Java approach, as proposed JDBC driver has native libraries bundled there, but I wouldn't consider this as an issue.