2017-08-10 14 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

相關問題