I’m encountering an issue with Flyway in my Spring application. The error message is:
'flywayQuranDatabase' defined in class path resource [com/LearnTheQuran/configuration/LTQDatabaseConfiguration.class]: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flywayQuranDatabase' threw exception with message: No database found to handle jdbc:postgresql://localhost:5432/quran
Here’s my configuration:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.LearnTheQuran.repository", transactionManagerRef = "quranJpaTransactionManager", entityManagerFactoryRef = "quranEntityManagerFactoryRef")
@EnableJpaAuditing
public class LTQDatabaseConfiguration {
@Bean(name = "quranDataSource")
@Primary
@ConfigurationProperties(prefix = "datasource")
public DataSource quranDataSource() {
return DataSourceBuilder.create().build();
}
@DependsOn("flyway")
@Bean("quranEntityManagerFactoryRef")
public LocalContainerEntityManagerFactoryBean quranEntityManagerFactoryRef() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.POSTGRESQL);
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(quranDataSource());
em.setPackagesToScan("com.LearnTheQuran.model");
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaPropertyMap(hibernateProperties());
em.afterPropertiesSet();
return em;
}
private Map<String, Object> hibernateProperties() {
Resource resource = new ClassPathResource("hibernate.properties");
try {
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
return properties.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue()));
} catch (IOException e) {
return new HashMap<String, Object>();
}
}
@Bean(name = "flywayConfiguration")
@ConfigurationProperties(prefix = "spring.flyway")
public ClassicConfiguration flywayConfiguration() {
return new ClassicConfiguration();
}
@Bean
public Flyway flywayQuranDatabase(ClassicConfiguration flywayConfiguration) {
Flyway flyway = Flyway.configure().configuration(flywayConfiguration).dataSource(this.quranDataSource()).load();
return flyway;
}
@Bean(name="flyway")
public FlywayMigrationInitializer flyway(
@Qualifier("flywayQuranDatabase") Flyway flywayQuranDatabase) {
FlywayMigrationInitializer initializer = new FlywayMigrationInitializer(flywayQuranDatabase, null);
return initializer;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean("quranJpaTransactionManager")
public JpaTransactionManager quranJpaTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(quranEntityManagerFactoryRef().getObject());
return transactionManager;
}
@Bean
public AuditorAware<String> securityBasedAuditorAware() {
return new SpringSecurityAuditorAware();
}
}
application.yml
spring:
application:
name: Learn The Quran
datasource:
url: jdbc:postgresql://localhost:5432/quran
username: postgres
password: pwd
driver-class-name: org.postgresql.Driver
flyway:
enabled: true
locations: classpath:db/migration/db/postgres
schemas: public
baseline-on-migrate: true
url: jdbc:postgresql://localhost:5432/quran
user: postgres
password: pwd
I have verified that PostgreSQL is running and the database URL is correct. The org.flywaydb
dependency is included in my pom.xml
(2 child pom.xml have the dependency), and I can connect to the database using the same URL and credentials.
1