0

I am using HikariCP and jdbc template to query from one database.The method i use is like below:

My Hikari CP config file(hikari.properties):

driverClassName=org.postgresql.Driver
jdbcUrl=jdbc:postgresql://10.1.19.99:5432/mvc_data_base
maximumPoolSize=60
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048

I do the configuration by this

@Configuration
public class AppConfig {
    @Bean(destroyMethod = "close")
    public DataSource dataSource() throws SQLException {
    HikariConfig config = new HikariConfig("D:/hikari.properties");
    HikariDataSource dataSource = new HikariDataSource(config);
    return dataSource;
    }
    }

I want to query I do by the JDBC template

public class UserDetail {

    @Autowired
    private JdbcTemplate jtmUserDetail;
         List<Map<String, Object>> row1= 
           jtmUserDetail.queryForList("select * from pubic.user");

}

Now I want something like

 public class UserDetail {

    @Autowired
    private JdbcTemplate jtmUserDetail;
    @Autowired
    private JdbcTemplate jtmUserDetail2;
         List<Map<String, Object>> row1= 
           jtmUserDetail.queryForList("select * from pubic.user");
         List<Map<String, Object>> row2= 
           jtmUserDetail2.queryForList("select * from pubic.user");


}

where jtmUserDetail is querying from one database(mvc_data_base) and jtmUserDetail2 from another(test_data_base).

I am not sure how to specify which database to query here.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
joker21
  • 339
  • 2
  • 4
  • 14

1 Answers1

1

You need to have 2 datasources properties and 2 functions for getting each datasource as

 public DataSource dataSourceSecond() throws SQLException {
  HikariConfig config = new HikariConfig("D:/hikari2.properties");

and then add logic to control when to use each of the datasource.

You can use 2 jdbcTemplate with different @Qualifier

Ori Marko
  • 56,308
  • 23
  • 131
  • 233