Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 203
Ryan Avatar asked Nov 04 '14 20:11

Ryan


People also ask

What is JDBC URL in H2 database?

As per documentation, default JDBC connection string is jdbc:h2:~/test. And, for TCP connection jdbc:h2:tcp://localhost/~/test.

Can I use H2 database in production?

It can be embedded in Java applications or run in the client-server mode. Mainly, H2 database can be configured to run as inmemory database, which means that data will not persist on the disk. Because of embedded database it is not used for production development, but mostly used for development and testing.

What is H2 database server mode?

Ad. H2 DB is an open source lightweight Database written in Java. It is bundled in JBoss EAP and WildFly application server to speed up developing and testing Java applications.


2 Answers

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.

like image 127
Lolo Avatar answered Oct 27 '22 12:10

Lolo


AUTO_SERVER=TRUE allows multiple connections mode.

like image 44
Jackkobec Avatar answered Oct 27 '22 11:10

Jackkobec