Class HikariDataSourceBuilder

java.lang.Object
net.blockhost.commons.database.core.HikariDataSourceBuilder

public final class HikariDataSourceBuilder extends Object

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 Details

    • create

      public static HikariDataSourceBuilder create(DatabaseCredentials credentials)
      Creates a new builder with the specified credentials.
      Parameters:
      credentials - the database credentials
      Returns:
      a new builder instance
    • poolName

      public HikariDataSourceBuilder poolName(String 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

      public HikariDataSourceBuilder maximumPoolSize(int size)
      Sets the maximum size of the connection pool.
      Parameters:
      size - the maximum pool size (must be at least 1)
      Returns:
      this builder
    • minimumIdle

      public HikariDataSourceBuilder minimumIdle(int size)
      Sets the minimum number of idle connections in the pool.
      Parameters:
      size - the minimum idle connections (must be non-negative)
      Returns:
      this builder
    • maxLifetime

      public HikariDataSourceBuilder maxLifetime(Duration maxLifetime)
      Sets the maximum lifetime of a connection in the pool.
      Parameters:
      maxLifetime - the maximum lifetime
      Returns:
      this builder
    • idleTimeout

      public HikariDataSourceBuilder idleTimeout(Duration idleTimeout)
      Sets the maximum time a connection can be idle before being retired.
      Parameters:
      idleTimeout - the idle timeout
      Returns:
      this builder
    • connectionTimeout

      public HikariDataSourceBuilder connectionTimeout(Duration connectionTimeout)
      Sets the maximum time to wait for a connection from the pool.
      Parameters:
      connectionTimeout - the connection timeout
      Returns:
      this builder
    • validationTimeout

      public HikariDataSourceBuilder validationTimeout(Duration validationTimeout)
      Sets the timeout for connection validation.
      Parameters:
      validationTimeout - the validation timeout
      Returns:
      this builder
    • cachePreparedStatements

      public HikariDataSourceBuilder cachePreparedStatements(boolean enabled)
      Enables or disables prepared statement caching.
      Parameters:
      enabled - true to enable caching
      Returns:
      this builder
    • preparedStatementCacheSize

      public HikariDataSourceBuilder preparedStatementCacheSize(int size)
      Sets the prepared statement cache size.
      Parameters:
      size - the cache size
      Returns:
      this builder
    • preparedStatementCacheSqlLimit

      public HikariDataSourceBuilder preparedStatementCacheSqlLimit(int limit)
      Sets the maximum length of a prepared statement SQL that can be cached.
      Parameters:
      limit - the SQL length limit
      Returns:
      this builder
    • autoReconnect

      public HikariDataSourceBuilder autoReconnect(boolean enabled)
      Enables or disables auto-reconnect.
      Parameters:
      enabled - true to enable auto-reconnect
      Returns:
      this builder
    • build

      public HikariDataSource build()
      Builds the HikariDataSource with the configured settings.
      Returns:
      a new HikariDataSource instance