title: How to use HikariCP in Spring Boot with two datasources in conjunction with Flyway
body: <p>I want to use HikariCP as JDBC connection pool in my Spring boot application. I have two datasources (MySQL database as the primary database and accessing those data through Hibernate and additionally an Oracle database for reading some other data through JDBCTemplate).</p>
  
  <p>I set the MySQL datasource as primary bean:</p>
  
  <pre><code>@Bean
  @Primary
  @ConfigurationProperties("spring.datasource")
  public DataSourceProperties mySQLDataSourceProperties() {
      return new DataSourceProperties();
  }
  
  
  @Bean
  @Primary
  @ConfigurationProperties("spring.datasource")
  public DataSource mySQLDataSource() {
      return mySQLDataSourceProperties().initializeDataSourceBuilder().build();
  }
  
  @Bean
  @ConfigurationProperties("oracle.datasource")
  public DataSourceProperties oracleDataSourceProperties() {
      return new DataSourceProperties();
  }
  
  @Bean(name = "oracleDatabase")
  @ConfigurationProperties("oracle.datasource")
  public DataSource oracleDataSource() {
      return oracleDataSourceProperties().initializeDataSourceBuilder().build();
  }
  
   @Bean
   public JdbcTemplate oracleJdbcTemplate(@Qualifier("oracleDatabase") DataSource oracleDb) {
       return new JdbcTemplate(oracleDb);
   }
  </code></pre>
  
  <p>and I put the following configurations in my application.properties : </p>
  
  <pre><code>spring.datasource.type=com.zaxxer.hikari.HikariDataSource
  
  spring.datasource.hikari.minimum-idle=7
  spring.datasource.hikari.pool-name=Test-1
  
  spring.datasource.hikari.data-source-properties.prepStmtCacheSize=250
  spring.datasource.hikari.data-source-properties.prepStmtCacheSqlLimit=2048
  spring.datasource.hikari.data-source-properties.cachePrepStmts=true
  spring.datasource.hikari.data-source-properties.useServerPrepStmts=true
  </code></pre>
  
  <p>Unforuntately, these HikariCP configurations are not being read :</p>
  
  <pre><code> HikariConfig - dataSourceJNDI..................none
   HikariConfig - dataSourceProperties............{password=&lt;masked&gt;}
   HikariConfig - driverClassName................."com.mysql.jdbc.Driver"
   HikariConfig - healthCheckProperties...........{}
   HikariConfig - healthCheckRegistry.............none
   HikariConfig - idleTimeout.....................600000
   HikariConfig - initializationFailFast..........true
   HikariConfig - initializationFailTimeout.......1
   HikariConfig - isolateInternalQueries..........false
   HikariConfig - jdbc4ConnectionTest.............false
   HikariConfig - jdbcUrl........................."jdbc:mysql://localhost:3306/testDB"
   HikariConfig - leakDetectionThreshold..........0
   HikariConfig - maxLifetime.....................1800000
   HikariConfig - maximumPoolSize.................10
   HikariConfig - metricRegistry..................none
   HikariConfig - metricsTrackerFactory...........none
   HikariConfig - minimumIdle.....................10
   HikariConfig - password........................&lt;masked&gt;
   HikariConfig - poolName........................"HikariPool-1"
  </code></pre>
  
  <p>Creating the HikariCP beans and deactivating the DataSource autoconfiguration and removing  "spring.datasource" : </p>
  
  <pre><code>@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
  @SpringBootApplication
  @ComponentScan
  public class SpringApplication {
  
  
  @Bean
  @Primary
  @ConfigurationProperties(prefix = "spring.datasource.hikari")
  public HikariConfig hikariConfig() {
      return new HikariConfig();
  }
  
  @Bean
  public DataSource dataSource() {
      return new HikariDataSource(hikariConfig());
  }
  </code></pre>
  
  <p>solves my problem :</p>
  
  <pre><code> HikariConfig - dataSourceJNDI..................none
   HikariConfig - dataSourceProperties............{password=&lt;masked&gt;, prepStmtCacheSqlLimit=2048, cachePrepStmts=true, useServerPrepStmts=true, prepStmtCacheSize=250}
   HikariConfig - driverClassName................."com.mysql.jdbc.Driver"
   HikariConfig - healthCheckProperties...........{}
   HikariConfig - healthCheckRegistry.............none
   HikariConfig - idleTimeout.....................600000
   HikariConfig - initializationFailFast..........true
   HikariConfig - initializationFailTimeout.......1
   HikariConfig - isolateInternalQueries..........false
   HikariConfig - jdbc4ConnectionTest.............false
   HikariConfig - jdbcUrl........................."jdbc:mysql://localhost:3306/testDB?autoReconnect=true"
   HikariConfig - leakDetectionThreshold..........0
   HikariConfig - maxLifetime.....................1800000
   HikariConfig - poolName........................"Test-1"
  </code></pre>
  
  <p>But then the Flyway showing some weird warnings which were not shown before and I have to create the database Schema manually before running the Spring application, that is : the create schema does not work anymore.</p>
  
  <pre><code>[WARN ] JdbcTemplate - DB: Can't create database 'test'; database exists (SQL State: HY000 - Error Code: 1007)
  [WARN ] JdbcTemplate - DB: Unknown table 'testSchema.tenant' (SQL State: 42S02 - Error Code: 1051)
  [WARN ] JdbcTemplate - DB: Unknown table 'testSchema.user' (SQL State: 42S02 - Error Code: 1051) 
  </code></pre>
  
  <p>My Flyway SQL scripts are plain DDL scripts :</p>
  
  <pre><code>CREATE SCHEMA IF NOT EXISTS `testSchema` DEFAULT CHARACTER SET utf8 ;
  
  DROP TABLE IF EXISTS `testSchema`.`tenant`;
  
  CREATE TABLE `testSchema`.`tenant` (
    `id` int NOT NULL AUTO_INCREMENT,
  </code></pre>
  
  <p>I think that disabling the Auto-Datasource configuration is not the best solution since Flyway stops creating the schema and showing warnings.  Is there any other way to solve this ?</p>