JdbcTemplate binding to multiple datasources

38 views Asked by At

I am trying to add a second data-source to a spring Boot project.

The primary connection is configured using spring.datasource.* properties, which I are getting configured using DataSourceAutoConfiguration. The entire project makes use of JdbcTemplate and no JPA config is required.

If I am creating an @Configuration for secondary datasource and excluding default datasource injected packages, the secondary configuration is overriding the default datasource which will be configured using datasourceautoconfiguration class

Is what I am trying to do possible?

I tried building both datasources seperately and autowiring the JdbcTemplate based on qualifier -> This works.

I tried only binding the second datatsource and autowired using qualifier -> this takes second datasource for the primary jdbcTemplate as well. Since first source is configured using DataSourceAutoConfiguration, I am uanble to bind this source to a template.

P.S: sorry I cannot paste the code here and default config should be untouched.

1

There are 1 answers

1
Prasahnth Joshi On

I could not achieve the above requirement using @Configuration but the alternative solution what I did was, build the DataSource in a @Service and bind that to the local JdbcTemplate where I wanted to use that secondary connection

Edit: As per @M.Deniums' suggestion in the comment, this should not be done.