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?
As per documentation, default JDBC connection string is jdbc:h2:~/test. And, for TCP connection jdbc:h2:tcp://localhost/~/test.
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.
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.
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.
AUTO_SERVER=TRUE allows multiple connections mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With