0

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

zerayaqob
  • 426
  • 1
  • 6
  • 12
  • Please look at my answer [Spring with MyBatis: expected single matching bean but found 2](http://stackoverflow.com/a/36842458/5619827). It could be help. This will help you to build real multiple data sources in single application. – Horsing May 01 '16 at 02:34

1 Answers1

0

You can add your own test's configuration with h2 database but use other database in production. You've excluded DataSourceAutoConfiguration in your spring application class, but it's usefull in your integration tests.

One simple solution is using a different spring application class only for your integrating tests without excluding DataSourceAutoConfiguration or just implement your own DataSourceAutoConfiguration and enable it. You can decide when will your own DataSourceAutoConfiguration be enabled or not within your own implementation. For example, if there are junit or spring-test exists in your classpath it will be enabled, otherwise will be disabled. This requires the junit or spring-test not going to be included in production, which can be handled by dependencies management such as maven.

Horsing
  • 1,070
  • 7
  • 22