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


in project, have created 3 spring boot application. first spring boot application has h2 embedded database. want access database 2nd , 3rd spring boot application directly without writing services data. can tell me how can achieve this?

you can setup h2 server spring bean.

edit pom.xml (delete <scope>runtime</scope>):

<dependency>     <groupid>com.h2database</groupid>     <artifactid>h2</artifactid> </dependency> 

add h2 server bean springbootapplication or configuration class:

@springbootapplication public class application {      public static void main(string[] args) {         springapplication.run(application.class, args);     }      /**      * start internal h2 server can query db ide      *      * @return h2 server instance      * @throws sqlexception      */     @bean(initmethod = "start", destroymethod = "stop")     public server h2server() throws sqlexception {         return server.createtcpserver("-tcp", "-tcpallowothers", "-tcpport", "9092");     } } 

edit application.properties - set name of 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 connect h2 server using connection: jdbc:h2:tcp://localhost:9092/mem:dbname

as bonus can connect database ide.

update

there chance of getting error when trying connect h2 spring boot app of 1.5.x version. in case change version of h2 previous one, example:

<dependency>     <groupid>com.h2database</groupid>     <artifactid>h2</artifactid>     <version>1.4.193</version> </dependency> 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -