How do I allow multiple users to connect to my H2 database simultaneously?

Ryan picture Ryan · Nov 4, 2014 · Viewed 19.4k times · Source

I am using H2 for database management, and this is what I would like to do:

I would like to allow multiple users to access a database at the same time. I've read a bit about "MULTI_THREADED=TRUE", "LOCK_FILE=NO", and "AUTO_SERVER=TRUE". I've also read that "LOCK_FILE=NO" can be dangerous, because it can corrupt the database. I definitely would not want this, so I'm assuming that is a bad way to go. I've also tried to close the connection immediately after a record is accessed, whether it is being read from or written to. So far, nothing seems to work. The application is not allowing me to read from or write to the database if the database has been connected to in a separate instance of the application (ex: on another computer). Once I completely close the application on one computer, I am able to access the database records.

How do I allow multiple users to connect to the H2 database at the same time without compromising the safety of the database?

Answer

Lolo picture Lolo · Nov 4, 2014

It looks like you are using H2 in embedded mode, which only allows one database connection at a time. See connection modes in the documentation for details.

If you need support for multiple connections, including from multiple application instances, then you need to start H2 in server mode instead and use the appropriate connection URLs for this mode.