[java] How do I configure HikariCP in my Spring Boot app in my application.properties files?

This works for my boot application in case it helps. This class tells you what properties the config object is looking for:

https://github.com/brettwooldridge/HikariCP/blob/2.3.x/hikaricp-common/src/main/java/com/zaxxer/hikari/AbstractHikariConfig.java

I think multiple datasources could be supporting by adding datasource_whatever to the property keys in the source config file. Cheers!

@Configuration
class DataSourceConfig {

   @Value('${spring.datasource.username}')
   private String user;

   @Value('${spring.datasource.password}')
   private String password;

   @Value('${spring.datasource.url}')
   private String dataSourceUrl;

   @Value('${spring.datasource.dataSourceClassName}')
   private String dataSourceClassName;

   @Value('${spring.datasource.connectionTimeout}')
   private int connectionTimeout;

   @Value('${spring.datasource.maxLifetime}')
   private int maxLifetime;

   @Bean
   public DataSource primaryDataSource() {
      Properties dsProps = [url: dataSourceUrl, user: user, password: password]
      Properties configProps = [
            connectionTestQuery: 'select 1 from dual',
            connectionTimeout: connectionTimeout,
            dataSourceClassName: dataSourceClassName,
            dataSourceProperties: dsProps,
            maxLifetime: maxLifetime
      ]

      // A default max pool size of 10 seems reasonable for now, so no need to configure for now.
      HikariConfig hc = new HikariConfig(configProps)
      HikariDataSource ds = new HikariDataSource(hc)
      ds
   }
}

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to spring-boot

Access blocked by CORS policy: Response to preflight request doesn't pass access control check Why am I getting Unknown error in line 1 of pom.xml? Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured How to resolve Unable to load authentication plugin 'caching_sha2_password' issue ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName ERROR Source option 1.5 is no longer supported. Use 1.6 or later How to start up spring-boot application via command line? JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value

Examples related to hikaricp

HikariCP - connection is not available How do I configure HikariCP in my Spring Boot app in my application.properties files? How to set up datasource with Spring for HikariCP?