I want to use h2 database for multiple spring boot applications. I have already read a couple of articles where they tell you how to configure h2 db, but with in-memory database where the data storage is temporary. I just want to know if it is possible to have persistent storage, with multiple spring boot applications for h2 db. Any help or link would be appreciated. Thanks.
2 Answers
It seems that there is a way for multiple applications to have access to the same file for H2 database (not in memory database) using the property AUTO_SERVER=TRUE in the end of the datasource.url. This is called Automatic Mixed Mode for H2 database
Automatic Mixed Mode
Multiple processes can access the same database without having to start the server manually. To do that, append ;AUTO_SERVER=TRUE to the database URL. You can use the same database URL independent of whether the database is already open or not. This feature doesn't work with in-memory databases. Example database URL:
jdbc:h2:/data/test;AUTO_SERVER=TRUE Use the same URL for all connections to this database. Internally, when using this mode, the first connection to the database is made in embedded mode, and additionally a server is started internally (as a daemon thread). If the database is already open in another process, the server mode is used automatically. The IP address and port of the server are stored in the file .lock.db, that's why in-memory databases can't be supported.
The application that opens the first connection to the database uses the embedded mode, which is faster than the server mode. Therefore the main application should open the database first if possible. The first connection automatically starts a server on a random port. This server allows remote connections, however only to this database (to ensure that, the client reads .lock.db file and sends the random key that is stored there to the server). When the first connection is closed, the server stops. If other (remote) connections are still open, one of them will then start a server (auto-reconnect is enabled automatically).
All processes need to have access to the database files. If the first connection is closed (the connection that started the server), open transactions of other connections will be rolled back (this may not be a problem if you don't disable autocommit). Explicit client/server connections (using jdbc:h2:tcp:// or ssl://) are not supported. This mode is not supported for in-memory databases.
Here is an example how to use this mode. Application 1 and 2 are not necessarily started on the same computer, but they need to have access to the database files. Application 1 and 2 are typically two different processes (however they could run within the same process).
The problem OP faced was as he commented:
I am able to connect to the h2 database in my main application using file storage. However when I try to connect from my 2nd application to the same db, then the file lock error comes up
Reason was that this is allowed only on Automatic Mixed Mode which OP has not enabled.
8 Comments
DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE; from the second url. It is not responsible for DB manipulation the second appspring.datasource.url=jdbc:h2:file:D:/ATOP_BACKEND/database/db;AUTO_SERVER=TRUE . If it works I can explain why it works thenh2 can store data in a file or in-memory. For file based storage, the jdbc url should be in the format jdbc:h2:[file:][]. For in-memory, the jdbc url looks like jdbc:h2:mem:. See http://www.h2database.com/html/features.html#database_url for details on the database url format.