Class HikariDataSourceBuilder
java.lang.Object
net.blockhost.commons.database.core.HikariDataSourceBuilder
Builder for creating HikariCP connection pool data sources.
This builder provides a fluent API for configuring HikariCP connection pools with sensible defaults. It works with any supported
invalid reference
DriverType
Example usage:
DatabaseCredentials credentials = DatabaseCredentials.builder()
.driverType(DriverType.MARIADB)
.host("localhost")
.database("mydb")
.username("user")
.password("pass")
.build();
HikariDataSource dataSource = HikariDataSourceBuilder.create(credentials)
.poolName("MyApp-Pool")
.maximumPoolSize(10)
.build();
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionautoReconnect(boolean enabled) Enables or disables auto-reconnect.build()Builds the HikariDataSource with the configured settings.cachePreparedStatements(boolean enabled) Enables or disables prepared statement caching.connectionTimeout(Duration connectionTimeout) Sets the maximum time to wait for a connection from the pool.static HikariDataSourceBuildercreate(DatabaseCredentials credentials) Creates a new builder with the specified credentials.idleTimeout(Duration idleTimeout) Sets the maximum time a connection can be idle before being retired.maximumPoolSize(int size) Sets the maximum size of the connection pool.maxLifetime(Duration maxLifetime) Sets the maximum lifetime of a connection in the pool.minimumIdle(int size) Sets the minimum number of idle connections in the pool.Sets the name of the connection pool.preparedStatementCacheSize(int size) Sets the prepared statement cache size.preparedStatementCacheSqlLimit(int limit) Sets the maximum length of a prepared statement SQL that can be cached.validationTimeout(Duration validationTimeout) Sets the timeout for connection validation.
-
Method Details
-
create
Creates a new builder with the specified credentials.- Parameters:
credentials- the database credentials- Returns:
- a new builder instance
-
poolName
Sets the name of the connection pool.
This name will appear in thread names and JMX metrics.
- Parameters:
poolName- the pool name- Returns:
- this builder
-
maximumPoolSize
Sets the maximum size of the connection pool.- Parameters:
size- the maximum pool size (must be at least 1)- Returns:
- this builder
-
minimumIdle
Sets the minimum number of idle connections in the pool.- Parameters:
size- the minimum idle connections (must be non-negative)- Returns:
- this builder
-
maxLifetime
Sets the maximum lifetime of a connection in the pool.- Parameters:
maxLifetime- the maximum lifetime- Returns:
- this builder
-
idleTimeout
Sets the maximum time a connection can be idle before being retired.- Parameters:
idleTimeout- the idle timeout- Returns:
- this builder
-
connectionTimeout
Sets the maximum time to wait for a connection from the pool.- Parameters:
connectionTimeout- the connection timeout- Returns:
- this builder
-
validationTimeout
Sets the timeout for connection validation.- Parameters:
validationTimeout- the validation timeout- Returns:
- this builder
-
cachePreparedStatements
Enables or disables prepared statement caching.- Parameters:
enabled- true to enable caching- Returns:
- this builder
-
preparedStatementCacheSize
Sets the prepared statement cache size.- Parameters:
size- the cache size- Returns:
- this builder
-
preparedStatementCacheSqlLimit
Sets the maximum length of a prepared statement SQL that can be cached.- Parameters:
limit- the SQL length limit- Returns:
- this builder
-
autoReconnect
Enables or disables auto-reconnect.- Parameters:
enabled- true to enable auto-reconnect- Returns:
- this builder
-
build
Builds the HikariDataSource with the configured settings.- Returns:
- a new HikariDataSource instance
-