Class HikariDataSourceConfig
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:
- The
HikariCredentialsUpdaterreceives the new credentials - The updater stores the new credentials for future connections
- Existing connections are soft-evicted via
softEvictConnections() - 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidExplicitly closes the HikariDataSource on application shutdown.com.zaxxer.hikari.HikariDataSourcedataSource(com.zaxxer.hikari.HikariConfig hikariConfig, HikariCredentialsUpdater credentialsUpdater) Creates the primary HikariCP DataSource bean with credential rotation support.com.zaxxer.hikari.HikariConfigCreates the HikariCP configuration bean.Creates the HikariCP credentials updater bean for credential rotation support.
-
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 theHikariCredentialsUpdatervia 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:
- Sets the
HikariCredentialsUpdateras the credentials provider on the config - Creates the
HikariDataSourcefrom the config - 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 configurationcredentialsUpdater- the credentials updater that provides dynamic credentials- Returns:
- a configured HikariDataSource with credential rotation support
- Sets the
-
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
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
CredentialsProviderServiceto receive notifications when credentials are rotated.- Returns:
- a credentials updater initialized with the configured credentials
- See Also:
-