Search code examples
javahibernatespring-bootconnection-poolinghikaricp

How to configure and monitor HikariCP with Hibernate and Spring Boot


I have a simple Spring Boot project with Hibernate, I want to know if my configuration class of HikariCP and the application.properties are correct, and how can I check and monitor the connection pools ?

My configuration class :

@Configuration
class HikariCPConfig {

   @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.poolName}")
   private String poolName;

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

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

   @Value("${spring.datasource.maximumPoolSize}")
   private int maximumPoolSize;

   @Value("${spring.datasource.minimumIdle}")
   private int minimumIdle;

   @Value("${spring.datasource.idleTimeout}")
   private int idleTimeout;

   @Bean
   public HikariDataSource primaryDataSource() {
       Properties dsProps = new Properties();
       dsProps.put("url", dataSourceUrl);
       dsProps.put("user", user);
       dsProps.put("password", password);
       dsProps.put("prepStmtCacheSize",250);
       dsProps.put("prepStmtCacheSqlLimit",2048);
       dsProps.put("cachePrepStmts",Boolean.TRUE);
       dsProps.put("useServerPrepStmts",Boolean.TRUE);

       Properties configProps = new Properties();
          configProps.put("dataSourceClassName", dataSourceClassName);
          configProps.put("poolName",poolName);
          configProps.put("maximumPoolSize",maximumPoolSize);
          configProps.put("minimumIdle",minimumIdle);
          configProps.put("minimumIdle",minimumIdle);
          configProps.put("connectionTimeout", connectionTimeout);
          configProps.put("idleTimeout", idleTimeout);
          configProps.put("dataSourceProperties", dsProps);

      HikariConfig hc = new HikariConfig(configProps);
      HikariDataSource ds = new HikariDataSource(hc);
      return ds;
   }
}

application.properties file :

server.port= 8888
spring.jpa.databasePlatform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update

spring.datasource.dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/db_test
spring.datasource.username=root
spring.datasource.password=
spring.datasource.poolName=SpringBootHikariCP
spring.datasource.maximumPoolSize=5
spring.datasource.minimumIdle=3
spring.datasource.maxLifetime=2000000
spring.datasource.connectionTimeout=30000
spring.datasource.idleTimeout=30000
spring.datasource.pool-prepared-statements=true
spring.datasource.max-open-prepared-statements=250

With just this config class and the properties file, is it sure that HikariCP is working ? because I'm able to connect to the database even if I comment the whole configuration class, so I don't really know for sure.


Solution

  • Hikari is auto-configured, that's why it works when you remove your custom configuration class. Just drop HikariCPConfig.

    I guess it's no accident that you're using the spring.datasource namespace. Why don't you try to configure the existing feature rather than creating a configuration class yourself?

    You can check that Hikari is properly configured by inspecting the auto-configuration report