How to access h2 database from one spring boot application from another spring boot application

In my project, I created a 3 spring boot application. The first spring boot application has a built-in h2 database. Now I want to access this database from my second and third spring boot application directly, without creating any services to get this data. So can someone tell me how I can achieve this?

+6
source share
2 answers

You can install H2 Server as Spring Bean.

First edit pom.xml - remove the <scope>runtime</scope> from the h2 dependency:

 <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> 

Then add the H2 bean server to the SpringBootApplication or Configuration class:

 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } /** * Start internal H2 server so we can query the DB from IDE * * @return H2 Server instance * @throws SQLException */ @Bean(initMethod = "start", destroyMethod = "stop") public Server h2Server() throws SQLException { return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092"); } } 

Last - edit application.properties - set the database name:

 spring.datasource.url=jdbc:h2:mem:dbname spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.hibernate.ddl-auto=create 

Then you can connect to this H2 server from the outside (for example, to your application using H2 DB) using this connection:

 jdbc:h2:tcp://localhost:9092/mem:dbname 

As a bonus, using this URL you can connect to your application database directly from your IDE .

UPDATE

There is a chance of getting an error message when trying to connect to H2 for Spring boot application version 1.5.x. In this case, just change the version of H2 to the previous one, for example:

 <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.193</version> </dependency> 

UPDATE 2

If you need to run several applications with H2 on the same host at the same time, you must install different H2 ports for them in Server.createTcpServer mothod, for example: 9092, 9093, etc.

 // First App @Bean(initMethod = "start", destroyMethod = "stop") public Server h2Server() throws SQLException { return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092"); } // Second App @Bean(initMethod = "start", destroyMethod = "stop") public Server h2Server() throws SQLException { return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9093"); } 

You can then connect to the H2 DB of these applications with the following URLs:

 App1 H2: jdbc:h2:tcp://localhost:9092/mem:dbname App2 H2: jdbc:h2:tcp://localhost:9093/mem:dbname 
+18
source

You can run H2 in server mode.

 import org.h2.tools.Server; ... // start the TCP Server server = Server.createTcpServer("-tcpAllowOthers").start(); ... // stop the TCP Server server.stop(); Usage: java org.h2.tools.Server When running without options, -tcp, -web, -browser and -pg are started. Options are case sensitive. Supported options are: [-help] or [-?] Print the list of options [-web] Start the web server with the H2 Console [-webAllowOthers] Allow other computers to connect - see below [-webDaemon] Use a daemon thread [-webPort ] The port (default: 8082) [-webSSL] Use encrypted (HTTPS) connections [-browser] Start a browser connecting to the web server [-tcp] Start the TCP server [-tcpAllowOthers] Allow other computers to connect - see below [-tcpDaemon] Use a daemon thread [-tcpPort ] The port (default: 9092) [-tcpSSL] Use encrypted (SSL) connections [-tcpPassword ] The password for shutting down a TCP server [-tcpShutdown ""] Stop the TCP server; example: tcp://localhost [-tcpShutdownForce] Do not wait until all connections are closed [-pg] Start the PG server [-pgAllowOthers] Allow other computers to connect - see below [-pgDaemon] Use a daemon thread [-pgPort ] The port (default: 5435) [-properties ""] Server properties (default: ~, disable: null) [-baseDir ] The base directory for H2 databases (all servers) [-ifExists] Only existing databases may be opened (all servers) [-trace] Print additional trace information (all servers) The options -xAllowOthers are potentially risky. For details, see Advanced Topics / Protection against Remote Access. See also http://h2database.com/javadoc/org/h2/tools/Server.html 

How to use h2 as a server

Similar question 1

Similar question 2

0
source

Source: https://habr.com/ru/post/1266418/


All Articles