Home > Software engineering >  Spring Boot Hikari properties override programmatically
Spring Boot Hikari properties override programmatically

Time:04-15

I have two datasources in my Spring Boot app and therefore I cannot use the standard datasource properites. One of them is defined like below:

vertica:
  datasource:
    jdbc-url: jdbc:vertica://${DR_HOST}:${DR_PORT}/${DR_DB_NAME}
    username: ${DR_USER}
    password: ${DR_PASSWORD}
    driver-class-name: com.vertica.jdbc.Driver
    hikari:
      connectionTimeout: 30000
      idleTimeout: 10000
      maxLifetime: 10000
      keepaliveTime: 5000
      maximumPoolSize: 20
      minimumIdle: 5
      #poolName: vertica-db-pool
      #username: ${DB_USER}
      #password: ${DB_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: none
      format_sql: true
      show-sql: true
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
      naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
    properties:
      hibernate:
        dialect: org.hibernate.dialect.VerticaDialect
@Configuration
@ConfigurationProperties("vertica.datasource")
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "verticaEntityManagerFactory",
        transactionManagerRef = "verticaTransactionManager",
        basePackages = { "com.yyy.vertica" }
)
public class MyDataSourceConfig extends HikariConfig {
    public final static String PERSISTENCE_UNIT_NAME = "vertica";
    public final static String PACKAGES_TO_SCAN = "com.xxx.entity";

    @Autowired
    private Environment env;

    @Bean
    public HikariDataSource verticaDataSource() {
        return new HikariDataSource(this);
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean verticaEntityManagerFactory(
            final HikariDataSource verticaDataSource) {

        return new LocalContainerEntityManagerFactoryBean() {{
            setDataSource(verticaDataSource);
            setPersistenceProviderClass(HibernatePersistenceProvider.class);
            setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
            setPackagesToScan(PACKAGES_TO_SCAN);

            Properties jpaProperties = new Properties();
            jpaProperties.put("hibernate.ddl-auto", env.getProperty("vertica.jpa.hibernate.ddl-auto"));
            jpaProperties.put("hibernate.show-sql", env.getProperty("vertica.jpa.hibernate.show-sql"));
            jpaProperties.put("hibernate.format_sql", env.getProperty("vertica.jpa.hibernate.format_sql"));
            jpaProperties.put("hibernate.dialect", env.getProperty("vertica.jpa.properties.hibernate.dialect"));

            setJpaProperties(jpaProperties);

            afterPropertiesSet();;
        }};
    }

    @Bean
    public PlatformTransactionManager verticaTransactionManager(EntityManagerFactory verticaEntityManagerFactory) {
        return new JpaTransactionManager(verticaEntityManagerFactory);
    }

The Hikari properties defined in yaml file above are not taken into account (possibly due to extends HikariConfig). The requirement is to use the default Hikari properties and override the ones defined above. Please suggest how this can be done.

CodePudding user response:

Do not extend HikariConfig.

Use the default constructor of HikariConfig() to take the default configuration properties of Hikari and then override what you need according to your properties file as you have already done for your jpa properties.

@Bean
public HikariDataSource verticaDataSource() {
 HikariConfig hikariConfig = new HikariConfig();
        
hikariConfig.setConnectionTimeout(env.getProperty("vertica.datasource.hikari.connectionTimeout", Long.class)); 
  
hikariConfig.setIdleTimeout(env.getProperty("vertica.datasource.hikari.idleTimeout", Long.class)); 

hikariConfig.setMaxLifetime(env.getProperty("vertica.datasource.hikari.maxLifetime", Long.class)); 

hikariConfig.setKeepaliveTime(env.getProperty("vertica.datasource.hikari.keepaliveTime", Long.class));

hikariConfig.setMaximumPoolSize​(env.getProperty("vertica.datasource.hikari.maximumPoolSize", Integer.class));

hikariConfig.setMinimumIdle​(env.getProperty("vertica.datasource.hikari.minimumIdle", Integer.class));

return new HikariDataSource(hikariConfig);

}
  • Related