Spring boot - Issue with multiple DataSource -


i have rest service needs fetch data 2 different dbs (oracle , mysql) , merge data in response.

i have below configuration.

config 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 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(*) employee", integer.class));          object[] parameters = new object[] { id };         string name = jdbc.queryforobject(                 "select name employee id = ?", parameters,                 string.class);         return name;     }  } 

i not able start app below error:

exception in thread "main" org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.boot.autoconfigure.jdbc.datasourcetransactionmanagerautoconfiguration': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private javax.sql.datasource org.springframework.boot.autoconfigure.jdbc.datasourcetransactionmanagerautoconfiguration.datasource;   nested exception org.springframework.beans.factory.nouniquebeandefinitionexception: no qualifying bean of type [javax.sql.datasource] defined: expected single matching bean found 2: brmdatasource,siebeldatasource 

the issue 2 datasource beans in context. how resolve this?

you mark 1 of them @primary spring boot auto configuration transactions manager know 1 pick. if need manage transactions both of them i'm afraid have setup transactions management explicitly.

please refer spring boot documentation

creating more 1 data source works same creating first one. might want mark 1 of them @primary if using default auto-configuration jdbc or jpa (then 1 picked @autowired injections).


Comments