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

前端 未结 2 994
陌清茗
陌清茗 2020-11-29 08:49

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 sprin

相关标签:
2条回答
  • 2020-11-29 09:14

    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
    
    0 讨论(0)
  • 2020-11-29 09:41

    You can run H2 in the 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 讨论(0)
提交回复
热议问题