Reputation: 531
I have asked questions regarding this, but this is a bit different.
Right now, I have my program set up to insert a query (whether it is to read from or write to the database), commit the changes, and immediately close the connection. This allows for the database to be locked for a very short amount of time. I am using H2 for my database, and I have "AUTO_SERVER=TRUE" set, so that the database can run over the network and allow multiple connections simultaneously.
Now, here is my dilemma. If, for some reason, a user attempts to access the database at the exact time that the lock file is present, how should I handle it? Should I display a JOptionPane that notifies them that the database is currently locked by another user, and that they should ensure that all other connections are closed and try again? If so, what Java code would I use to determine whether or not the lock file exists (or, simply if the database IS indeed locked)?
As usual. thank you in advance for any helpful responses.
Upvotes: 0
Views: 608
Reputation: 136
The H2 daemon thread is creating the lock to avoid having another daemon thread come in and try to operate on the database file while it is currently in use. If you are using Auto Server mode then the H2 daemon thread will check for the existence of a lock and if it is there it will try to connect to the server specified in the lock file.
If two users try to connect to the same database, the first will gain the lock and start the H2 server, the second will connect to the server that the first one has started.
Check out the documentation on auto mixed mode
Upvotes: 0
Reputation: 76
Maybe i'm wrong but if your H2 base is in server mode you don't have any lock and can connect multiple user at the same time (you must configure your datasource for server mode and not embedded mode).
Upvotes: 1