Skip to content

Instantly share code, notes, and snippets.

@syakuis
Created March 20, 2019 07:33
Show Gist options
  • Save syakuis/7bc8f683426f27af1788794172172802 to your computer and use it in GitHub Desktop.
Save syakuis/7bc8f683426f27af1788794172172802 to your computer and use it in GitHub Desktop.
DB 서버에 접속하지 못하면 특정 시간이후에 flyway 를 다시 시작하도록 작업
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.internal.dbsupport.FlywaySqlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import java.util.Objects;
/**
* 서비스 재시작 DB 보다 먼저 flyway 가 실행되면서 연결오류로 인해 문제가 발생하는 부분을 해결하기 위해
* DB 서버에 접속하지 못하면 특정 시간이후에 flyway 를 다시 시작하도록 작업
* @author Seok Kyun. Choi.
* @since 2019-03-20Ø
*/
@Slf4j
@Configuration
public class FlywayConfig {
@Autowired
private Environment environment;
@Bean
public FlywayMigrationStrategy flywayMigrationStrategy() {
long maxWait = Long.valueOf(Objects.requireNonNull(environment.getProperty("spring.flyway.max-wait")));
return flyway -> {
try {
log.debug("--------------------------------------> Load flyway ...");
flyway.migrate();
} catch (FlywaySqlException e) {
log.error(e.getMessage(), e);
try {
log.debug("--------------------------------------> Reload the 'flyway' after ({}) milliseconds....", maxWait);
Thread.sleep(maxWait);
flyway.migrate();
} catch (InterruptedException ie) {
log.error(e.getMessage(), e);
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment