如何在 Spring 启动应用程序和 flyway 中启动 H2 db TCP 服务器
How to start H2 db TCP server within Spring Boot application and with flyway
我使用 Spring Boot 和 H2 db with Flyway,我想在启动我的应用程序时启动 H2 db tcp 服务器(写在 Spring Boot 中)。
所以我有这样的 application.properties 文件。
db.port=9090
spring.datasource.url=jdbc:h2:tcp://localhost:${db.port}/./database/ifin-relax
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.driver-class-name=org.h2.Driver
flyway.baseline-on-migrate=true
flyway.url=jdbc:h2:tcp://localhost:${db.port}/./database/ifin-relax
flyway.table=SCHEMA_VERSION
flyway.user=sa
flyway.password=password
而且我对 h2 数据库服务器有以下配置 class。
@Configuration
public class H2DBServerConfiguration {
@Value("${db.port}")
private String h2DbPort;
@Bean
public Server server() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", h2DbPort).start();
}
}
但是当我 运行 应用程序失败并出现异常时
Error creating bean with name 'flywayInitializer' defined in class path resource
flyway 似乎甚至在 H2 的 TCP 服务器实例化之前就尝试应用迁移。所以问题是如何将 flyway 迁移推迟到 DB Server 启动?
我找到了解决方案:
@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;
}
}
我使用 Spring Boot 和 H2 db with Flyway,我想在启动我的应用程序时启动 H2 db tcp 服务器(写在 Spring Boot 中)。
所以我有这样的 application.properties 文件。
db.port=9090
spring.datasource.url=jdbc:h2:tcp://localhost:${db.port}/./database/ifin-relax
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.driver-class-name=org.h2.Driver
flyway.baseline-on-migrate=true
flyway.url=jdbc:h2:tcp://localhost:${db.port}/./database/ifin-relax
flyway.table=SCHEMA_VERSION
flyway.user=sa
flyway.password=password
而且我对 h2 数据库服务器有以下配置 class。
@Configuration
public class H2DBServerConfiguration {
@Value("${db.port}")
private String h2DbPort;
@Bean
public Server server() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", h2DbPort).start();
}
}
但是当我 运行 应用程序失败并出现异常时
Error creating bean with name 'flywayInitializer' defined in class path resource
flyway 似乎甚至在 H2 的 TCP 服务器实例化之前就尝试应用迁移。所以问题是如何将 flyway 迁移推迟到 DB Server 启动?
我找到了解决方案:
@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;
}
}