1

If I use "FILE_LOCK=NO" in my H2 database, what is the danger? H2's web site says that I would need to protect the databases within the application. How do I do that? And what is the danger in disabling the file-lock?

Is there another way that I can allow multiple users to access the database(s) at the same time?

Thank you in advance for any helpful responses.

Ryan
  • 511
  • 1
  • 8
  • 18

1 Answers1

2

No, it is not safe to use FILE_LOCK=NO. In future version of H2, most likely this will no longer be supported.

The danger is that two applications at the same time open the same database file for writing, in which case the database will most likely get corrupt.

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
  • Okay. So, now I am using a method that will immediately commit changes and close the connection, deleting the lock file. Is this a safer route to go? Also, if, for some reason, a user tries to do something in the short amount of time that the lock file DOES exist, how should I handle it? A JOptionPane message dialogue? And how would I check for the existence of the lock file? – Ryan Nov 05 '14 at 16:59
  • 1
    Why do you delete the lock file? It will be automatically deleted when resources are released... to check the existence of a lock file: http://stackoverflow.com/questions/26768760/how-do-i-determine-if-an-h2-database-file-lock-exists – marcolopes Nov 06 '14 at 18:17
  • Yes, the lock file it automatically deleted and shouldn't be deleted manually (just to make sure there is no misunderstanding). If you can't open the database because it is locked, then one option is to re-try a few times, for example at most 10 times. Between re-tries, you could use a random wait time between 0-20 ms. This is similar to the way conflicts are handled in a network. – Thomas Mueller Nov 07 '14 at 09:34
  • if you are using it for development purposes, it is definitively justifiable and a useful feature. For example if you run your server against H2 DB and you wanna connect to the DB file from DBeaver to see the results without having to stop the server. Of course it is not safe, but I assume you are not using H2 in production environment – Jan Hruby Feb 24 '23 at 14:08