简体   繁体   中英

How to use 2 or more jdbcTemplate with spring-boot?

I want to use 2 or more jdbcTemplate in my project using application.properties.I try but got runtime exception.

########## My application.properties:-
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/ccm_new spring.datasource.username=test spring.datasource.password=test spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera spring.oracledatasource.password=test spring.oracledatasource.username=test spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver @Bean(name = "dsMaster") ############ @Primary @ConfigurationProperties(prefix="spring.oracledatasource") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "jdbcMaster") ############# public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster) { return new JdbcTemplate(dsMaster); } 
################I use the mysql connection normally but on use of oracle connection i got

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null' at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371) at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:446) at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456) at enter code here

I got it where i am wrong,I want to make mysql connection through application.properties without @bean configuration.If you want to take 2 or more connection you just need to define all the datasource with their @ConfigurationProperties(prefix="spring.mysqldatasource") different prifix other than "spring.datasource".prifix " spring.datasource" use only when we need to make connection from only one database.Here is the final working code example:-

application.properties

 spring.mysqldatasource.driver-class-name=com.mysql.jdbc.Driver
 spring.mysqldatasource.url=jdbc:mysql://localhost:3306/ccm_new
 spring.mysqldatasource.username=test
 spring.mysqldatasource.password=test
 spring.mysqldatasource.dbcp2.initial-size=5
 spring.mysqldatasource.dbcp2.max-total=15
 spring.mysqldatasource.dbcp2.pool-prepared-statements=true



spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.oracledatasource.dbcp2.initial-size=5
spring.oracledatasource.dbcp2.max-total=15
spring.oracledatasource.dbcp2.pool-prepared-statements=true



 @Configuration
 public class PrototypeUtility {
 @Bean(name = "dsMaster")
@Primary
@ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMaster")
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster) {
      return new JdbcTemplate(dsMaster);
}

@Bean(name = "dsMasterMysql")
@ConfigurationProperties(prefix="spring.mysqldatasource")
public DataSource primaryDataSourceMysql() {
    return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMasterMysql")
public JdbcTemplate masterMysqlJdbcTemplate(@Qualifier("dsMasterMysql") DataSource dsMasterMysql) {
      return new JdbcTemplate(dsMasterMysql);
}
  }

and then i autowired the both connection :-

     @Autowired
      private JdbcTemplate jdbcMasterMysql;

     @Autowired
     public JdbcTemplate jdbcMaster;

This code run successfully for me . If any one have doubt,Don't hesitate to ask.

I got it where i am wrong,I want to make mysql connection through application.properties without @bean configuration.If you want to take 2 or more connection you just need to define all the datasource with their @ConfigurationProperties(prefix="spring.mysqldatasource") different prifix other than "spring.datasource".prifix " spring.datasource" use only when we need to make connection from only one database.Here is the final working code example:-

application.properties

spring.mysqldatasource.driver-class-name=com.mysql.jdbc.Driver
 spring.mysqldatasource.url=jdbc:mysql://localhost:3306/ccm_new
 spring.mysqldatasource.username=test
 spring.mysqldatasource.password=test
 spring.mysqldatasource.dbcp2.initial-size=5
 spring.mysqldatasource.dbcp2.max-total=15
 spring.mysqldatasource.dbcp2.pool-prepared-statements=true



spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.oracledatasource.dbcp2.initial-size=5
spring.oracledatasource.dbcp2.max-total=15
spring.oracledatasource.dbcp2.pool-prepared-statements=true



 @Configuration
 public class PrototypeUtility {
 @Bean(name = "dsMaster")
@Primary
@ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMaster")
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster) {
      return new JdbcTemplate(dsMaster);
}

@Bean(name = "dsMasterMysql")
@ConfigurationProperties(prefix="spring.mysqldatasource")
public DataSource primaryDataSourceMysql() {
    return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMasterMysql")
public JdbcTemplate masterMysqlJdbcTemplate(@Qualifier("dsMasterMysql") DataSource dsMasterMysql) {
      return new JdbcTemplate(dsMasterMysql);
}
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM