0

I have a ReST service that needs to fetch data from two different DBs (Oracle and MySQL) and merge this data in the response.

I have below configuration.

Config for DB 1:

@Configuration 
public class DbConfig_DB1{

    @Bean(name="siebelDataSource")
    public EmbeddedDatabase siebelDataSource(){
        return new EmbeddedDatabaseBuilder().
                setType(EmbeddedDatabaseType.H2).
                addScript("schema.sql").
                addScript("test-data.sql").
                build();

    }

    @Autowired
    @Qualifier("siebelDataSource")
    @Bean(name = "siebelJdbcTemplate") 
    public JdbcTemplate siebelJdbcTemplate(DataSource siebelDataSource) { 
        return new JdbcTemplate(siebelDataSource); 
    } 
}

Config for DB2:

@Configuration 
public class DbConfig_DB2{      
    @Bean(name="brmDataSource")
    public EmbeddedDatabase brmDataSource(){
        return new EmbeddedDatabaseBuilder().
                setType(EmbeddedDatabaseType.H2).
                addScript("schema-1.sql").
                addScript("test-data-1.sql").
                build();

    }

    @Autowired
    @Qualifier("brmDataSource")
    @Bean(name = "brmJdbcTemplate") 
    public JdbcTemplate brmJdbcTemplate(DataSource brmDataSource) { 
        return new JdbcTemplate(brmDataSource); 
    } 
} 

Data Access:

@Repository
public class SiebelDataAccess {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    @Qualifier("siebelJdbcTemplate")
    protected JdbcTemplate jdbc;

    public String getEmpName(Integer id) {

        System.out.println(jdbc.queryForObject("select count(*) from employee", Integer.class));

        Object[] parameters = new Object[] { id };
        String name = jdbc.queryForObject(
                "select name from employee where id = ?", parameters,
                String.class);
        return name;
    }

}

I am not able to start the app as I below error:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration.dataSource; 

nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: brmDataSource,siebelDataSource

The issue is with two DataSource beans in the context. How to resolve this?

1 Answer 1

4

You could mark one of them as @Primary so Spring Boot auto configuration for transactions manager would know which one to pick. If you need to manage transactions with both of them then I'm afraid you would have to setup transactions management explicitly.

Please refer to the Spring Boot documentation

Creating more than one data source works the same as creating the first one. You might want to mark one of them as @Primary if you are using the default auto-configuration for JDBC or JPA (then that one will be picked up by any @Autowired injections).

Sign up to request clarification or add additional context in comments.

2 Comments

so to test I ran the same sql schema and contetns for both H2 instances. And obviously they are overwritten. Is there a way to have a two instances of H2 in memory?
Have you tried to call setName() on EmbeddedDatabaseBuilder? Otherwise it uses same DB name in both cases and executes schema scripts against same DB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.