How to start H2 TCP server on Spring Boot application startup?

后端 未结 5 1115
青春惊慌失措
青春惊慌失措 2021-01-05 06:44

I\'m able to start the H2 TCP server (database in a file) when running app as Spring Boot app by adding following line into the SpringBootServletInitializer main method:

5条回答
  •  忘掉有多难
    2021-01-05 06:55

    You can do like this:

    @Configuration
    public class H2ServerConfiguration {
    
      @Value("${db.port}")
      private String h2TcpPort;
    
      /**
       * TCP connection to connect with SQL clients to the embedded h2 database.
       *
       * @see Server
       * @throws SQLException if something went wrong during startup the server.
       * @return h2 db Server
       */
       @Bean
        public Server server() throws SQLException {
            return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", h2TcpPort).start();
       }
    
       /**
        * @return FlywayMigrationStrategy the strategy for migration.
        */
        @Bean
        @DependsOn("server")
        public FlywayMigrationStrategy flywayMigrationStrategy() {
            return Flyway::migrate;
        }
    }
    

提交回复
热议问题