Class HikariDataSourceConfig

java.lang.Object
com.maybeitssquid.rotatingsecrets.hikari.HikariDataSourceConfig

@Configuration public class HikariDataSourceConfig extends Object
Spring configuration for HikariCP DataSource with dynamic credential rotation support.

This configuration creates a HikariCP HikariDataSource as the primary application DataSource. HikariCP is a high-performance JDBC connection pool that is the default for Spring Boot applications. This configuration integrates HikariCP with the credential rotation system via HikariCredentialsUpdater.

Credential Rotation

Unlike standard HikariCP configurations that use static credentials, this configuration uses a HikariCredentialsProvider to support dynamic credentials. When credentials are rotated:

  1. The HikariCredentialsUpdater receives the new credentials
  2. The updater stores the new credentials for future connections
  3. Existing connections are soft-evicted via softEvictConnections()
  4. New connections automatically use the updated credentials

Configuration Properties

Standard Spring datasource properties:

  • spring.datasource.url - JDBC URL (required)
  • spring.datasource.driver-class-name - JDBC driver class (required)
  • spring.datasource.username - Initial database username (required)
  • spring.datasource.password - Initial database password (required)

HikariCP-specific properties (prefixed with spring.datasource.hikari.):

  • pool-name - Connection pool name (default: HikariPool)
  • maximum-pool-size - Maximum connections in pool (default: 10)
  • minimum-idle - Minimum idle connections (default: 2)
  • idle-timeout - Milliseconds before idle connection is removed (default: 30000)
  • connection-timeout - Milliseconds to wait for connection (default: 20000)
  • max-lifetime - Maximum connection lifetime in milliseconds (default: 1800000)
See Also:
  • Constructor Details

    • HikariDataSourceConfig

      public HikariDataSourceConfig()
      Default constructor.
  • Method Details

    • hikariConfig

      @Bean public com.zaxxer.hikari.HikariConfig hikariConfig()
      Creates the HikariCP configuration bean.

      This configuration is used to create the HikariDataSource. Note that while initial credentials are set here, the actual credentials used for connections come from the HikariCredentialsUpdater via the credentials provider interface.

      Returns:
      a configured HikariConfig
    • dataSource

      @Bean @Primary public com.zaxxer.hikari.HikariDataSource dataSource(com.zaxxer.hikari.HikariConfig hikariConfig, HikariCredentialsUpdater credentialsUpdater)
      Creates the primary HikariCP DataSource bean with credential rotation support.

      This method wires up the credential rotation infrastructure:

      1. Sets the HikariCredentialsUpdater as the credentials provider on the config
      2. Creates the HikariDataSource from the config
      3. Injects the DataSource back into the updater for connection eviction support

      The resulting DataSource is marked as @Primary, making it the default DataSource for the application when multiple DataSource beans exist.

      Parameters:
      hikariConfig - the HikariCP configuration
      credentialsUpdater - the credentials updater that provides dynamic credentials
      Returns:
      a configured HikariDataSource with credential rotation support
    • closeDataSource

      @PreDestroy public void closeDataSource()
      Explicitly closes the HikariDataSource on application shutdown.

      While Spring typically handles DataSource cleanup, explicit closure ensures all connections are properly released.

    • hikariCredentialsUpdater

      @Bean("hikariUpdater") public HikariCredentialsUpdater hikariCredentialsUpdater()
      Creates the HikariCP credentials updater bean for credential rotation support.

      The updater is initialized with the configured username and password, and is registered with the CredentialsProviderService to receive notifications when credentials are rotated.

      Returns:
      a credentials updater initialized with the configured credentials
      See Also: