2017-08-10 87 views
0

我使用Spring Boot和H2数据库与Flyway,并且我想在启动我的应用程序(用Spring Boot编写)时启动H2 db tcp服务器。如何在Spring Boot应用程序中启动H2 db TCP服务器并使用flyway

所以我有这样的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数据库服务器下面的配置类。

@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 

看来,迁徙路线尝试应用迁移H2的TCP服务器被实例化之前也。所以问题是如何推迟飞路迁移,直到数据库服务器启动?

+0

我不熟悉你的CONFIGS但是冲刺开机密码是不同的:密码=黄金。 –

+0

我的不好,编辑了密码。 – TimurJD

+0

对不起,我没有想法。你有更多的日志吗?也许打开调试。 –

回答

1

我找到了一个解决方案:

@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; 
    } 
} 
+0

请不要将[相同的答案](https://stackoverflow.com/a/45643148/4687348)添加到多个问题。回答最好的一个,并将其余标记为重复。请参阅[是否可以为几个问题添加重复答案?](http://meta.stackexchange.com/q/104227/347985) – FelixSFD

相关问题