I'm trying to set up Spring Boot to work with multiple datasources. I have followed the instructions for setting up two datasources and set one of them primary.
@Configuration
@EnableJpaRepositories(basePackages={"my.postgres.repositories"}
entityManagerFactoryRef="postgresEntitymanager"
transactionManagerRef="postgresTransactionManager")
public class PgConfig {
@Primary
@Bean(name="postgresDS")
@ConfigurationProperties(prefix="spring.datasource.postgres")
public DataSource postgresDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name="postgresEntityManager")
public LocalContainerEntityManagerFactoryBean postgresEntityManager(EntityManagerFactoryBuilder builder) {
return builder.dataSource(postgresDataSource())
.packages("my.postgres.domain")
.persistenceUnit("postgresPersistenceUnit")
.build();
}
@Primary
@Bean(name = "postgresTransactionManager")
public PlatformTransactionManager postgresTransactionManager(
@Qualifier("postgresEntityManager") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
I have a second configuration class for oracle which lacks the @Primary annotation but is very similar. I have also added this to my main class to exclude datasource auto configuration.
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
The problem that I'm facing is this setup doesn't allow my Integration tests that are supposed to run against an H2 databse to open a connection...
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection
Caused by: org.hibernate.exception.JDBCConnectionException: Could not open connection
Caused by: java.sql.SQLException: The url cannot be null
I'm using a separate application.properties file under src/integrationtest/resources which contains
spring.jpa.database=H2
How do I get my integration tests to use H2 for the repositories that I'm using when running my tests?
If I don't include my custom datasources everything seems to work fine.
Thanks