Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access in memory h2 database of one spring boot application from another spring boot application

In my project, i have created 3 spring boot application. First spring boot application has h2 embedded database. Now i want to access this database from my 2nd and 3rd spring boot application directly without writing any services to get this data. So can anyone tell me how can i achieve this?

like image 882
raj Avatar asked Apr 06 '17 13:04

raj


People also ask

How do I access my H2 memory database?

To access an in-memory database from another process or from another computer, you need to start a TCP server in the same process as the in-memory database was created. The other processes then need to access the database over TCP/IP or TLS, using a database URL such as: jdbc:h2:tcp://localhost/mem:db1 .

How does spring boot automatically connects to H2 database?

Spring Boot will automatically pick up this file and run it against an embedded in-memory database, such as our configured H2 instance. This is a good way to seed the database for testing or initialization purposes. We can disable this default behavior by setting the spring. sql.

How do I query H2 in spring boot?

H2 Console: By default, the console view of the H2 database is disabled. Before accessing the H2 database, we must enable it by using the following property. Once we have enabled the H2 console, now we can access the H2 console in the browser by invoking the URL http://localhost:8082/h2-console.


1 Answers

You can setup H2 Server as Spring Bean.

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

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

Then add H2 server bean to 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 name of the database:

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 outside (e.g. to your application with H2 DB) using this connection:

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

As a bonus using this url you can connect to the database of your app right from your IDE.

UPDATE

There is a chance of getting an error when trying to connect to the H2 for Spring Boot app of 1.5.x version. In this case just change a version of H2 to 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 apps with H2 simultaneously on the same host you should set the different H2 ports on 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");
}

Then you can connect to the H2 DB of these apps with following urls:

App1 H2: jdbc:h2:tcp://localhost:9092/mem:dbname
App2 H2: jdbc:h2:tcp://localhost:9093/mem:dbname
like image 65
Cepr0 Avatar answered Oct 08 '22 08:10

Cepr0