Ultimate JDBC Connection Pool - Java 6 compatible version providing high-performance database connection pooling
—
Native integration with Hibernate ORM framework, providing seamless connection pooling for Hibernate applications with automatic property mapping, lifecycle management, and support for Hibernate 4.3+ ConnectionProvider interface.
Hibernate 4.3+ ConnectionProvider implementation that integrates HikariCP as the connection pool for Hibernate applications.
public class HikariConnectionProvider implements ConnectionProvider, Configurable, Stoppable {
/**
* Default constructor.
*/
public HikariConnectionProvider();
/**
* Configure the connection provider from Hibernate properties.
* Properties with prefix "hibernate.hikari." are mapped to HikariConfig.
*
* @param props Hibernate configuration properties (raw Map type)
* @throws HibernateException on configuration error
*/
@SuppressWarnings("rawtypes")
public void configure(Map props) throws HibernateException;
/**
* Get connection from the HikariCP pool.
*
* @return Connection from the pool
* @throws SQLException if connection cannot be obtained
*/
public Connection getConnection() throws SQLException;
/**
* Close/return connection to the pool.
*
* @param conn connection to close
* @throws SQLException on error
*/
public void closeConnection(Connection conn) throws SQLException;
/**
* Returns false - HikariCP does not support aggressive connection release.
*
* @return false
*/
public boolean supportsAggressiveRelease();
/**
* Check if the provider can be unwrapped to the specified type.
*
* @param unwrapType target type
* @return true if unwrapping is supported
*/
public boolean isUnwrappableAs(Class unwrapType);
/**
* Unwrap the provider to the specified type.
*
* @param unwrapType target type
* @return unwrapped instance
*/
public <T> T unwrap(Class<T> unwrapType);
/**
* Stop the connection provider and shutdown the connection pool.
*/
public void stop();
}Utility class for mapping Hibernate properties to HikariCP configuration with proper property name translation and type conversion.
public class HikariConfigurationUtil {
/**
* Configuration property prefix for HikariCP properties in Hibernate configuration.
*/
public static final String CONFIG_PREFIX = "hibernate.hikari.";
/**
* Configuration property prefix for DataSource properties in Hibernate configuration.
*/
public static final String CONFIG_PREFIX_DATASOURCE = "hibernate.hikari.dataSource.";
/**
* Create HikariConfig from Hibernate properties map.
* Processes properties with "hibernate.hikari." prefix and maps them
* to corresponding HikariConfig properties.
*
* @param props Hibernate properties map (raw Map type)
* @return configured HikariConfig instance
*/
@SuppressWarnings("rawtypes")
public static HikariConfig loadConfiguration(Map props);
}<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Use HikariCP as connection provider -->
<property name="hibernate.connection.provider_class">
com.zaxxer.hikari.hibernate.HikariConnectionProvider
</property>
<!-- Database connection settings -->
<property name="hibernate.hikari.jdbcUrl">jdbc:postgresql://localhost/mydb</property>
<property name="hibernate.hikari.username">user</property>
<property name="hibernate.hikari.password">password</property>
<property name="hibernate.hikari.driverClassName">org.postgresql.Driver</property>
<!-- Pool configuration -->
<property name="hibernate.hikari.maximumPoolSize">20</property>
<property name="hibernate.hikari.minimumIdle">5</property>
<property name="hibernate.hikari.connectionTimeout">20000</property>
<property name="hibernate.hikari.idleTimeout">300000</property>
<property name="hibernate.hikari.maxLifetime">1200000</property>
<property name="hibernate.hikari.poolName">HibernatePool</property>
<!-- Hibernate settings -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
</session-factory>
</hibernate-configuration># Connection provider
hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider
# Database configuration
hibernate.hikari.jdbcUrl=jdbc:mysql://localhost:3306/myapp
hibernate.hikari.username=appuser
hibernate.hikari.password=apppass
hibernate.hikari.driverClassName=com.mysql.jdbc.Driver
# Pool sizing
hibernate.hikari.maximumPoolSize=25
hibernate.hikari.minimumIdle=5
# Timeouts (in milliseconds)
hibernate.hikari.connectionTimeout=30000
hibernate.hikari.idleTimeout=600000
hibernate.hikari.maxLifetime=1800000
# Pool management
hibernate.hikari.poolName=MyAppHibernatePool
hibernate.hikari.registerMbeans=true
hibernate.hikari.leakDetectionThreshold=60000
# DataSource properties (for DataSource-based configuration)
hibernate.hikari.dataSource.url=jdbc:mysql://localhost:3306/myapp
hibernate.hikari.dataSource.user=appuser
hibernate.hikari.dataSource.password=apppass
hibernate.hikari.dataSource.cachePrepStmts=true
hibernate.hikari.dataSource.prepStmtCacheSize=250
hibernate.hikari.dataSource.prepStmtCacheSqlLimit=2048
# Hibernate ORM settings
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.hbm2ddl.auto=validateimport org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.zaxxer.hikari.hibernate.HikariConnectionProvider;
// Create Hibernate Configuration
Configuration configuration = new Configuration();
// Set HikariCP as connection provider
configuration.setProperty("hibernate.connection.provider_class",
HikariConnectionProvider.class.getName());
// Database settings
configuration.setProperty("hibernate.hikari.jdbcUrl", "jdbc:h2:mem:testdb");
configuration.setProperty("hibernate.hikari.username", "sa");
configuration.setProperty("hibernate.hikari.password", "");
configuration.setProperty("hibernate.hikari.driverClassName", "org.h2.Driver");
// Pool configuration
configuration.setProperty("hibernate.hikari.maximumPoolSize", "10");
configuration.setProperty("hibernate.hikari.minimumIdle", "2");
configuration.setProperty("hibernate.hikari.connectionTimeout", "20000");
configuration.setProperty("hibernate.hikari.poolName", "H2TestPool");
// Hibernate settings
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop");
configuration.setProperty("hibernate.show_sql", "true");
// Add entity classes
configuration.addAnnotatedClass(User.class);
configuration.addAnnotatedClass(Order.class);
// Build SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
public class HibernateConfig {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
// Set packages to scan for entities
sessionFactory.setPackagesToScan("com.myapp.entities");
// Configure Hibernate properties
Properties hibernateProperties = new Properties();
// Use HikariCP connection provider
hibernateProperties.setProperty("hibernate.connection.provider_class",
"com.zaxxer.hikari.hibernate.HikariConnectionProvider");
// Database connection
hibernateProperties.setProperty("hibernate.hikari.jdbcUrl",
"jdbc:postgresql://localhost/myapp");
hibernateProperties.setProperty("hibernate.hikari.username", "appuser");
hibernateProperties.setProperty("hibernate.hikari.password", "apppass");
// Pool configuration
hibernateProperties.setProperty("hibernate.hikari.maximumPoolSize", "20");
hibernateProperties.setProperty("hibernate.hikari.minimumIdle", "5");
hibernateProperties.setProperty("hibernate.hikari.connectionTimeout", "30000");
hibernateProperties.setProperty("hibernate.hikari.poolName", "SpringHibernatePool");
hibernateProperties.setProperty("hibernate.hikari.registerMbeans", "true");
// Hibernate configuration
hibernateProperties.setProperty("hibernate.dialect",
"org.hibernate.dialect.PostgreSQLDialect");
hibernateProperties.setProperty("hibernate.show_sql", "false");
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");
sessionFactory.setHibernateProperties(hibernateProperties);
return sessionFactory;
}
}// Configure MySQL-specific DataSource properties for optimal performance
Properties hibernateProps = new Properties();
// Connection provider
hibernateProps.setProperty("hibernate.connection.provider_class",
"com.zaxxer.hikari.hibernate.HikariConnectionProvider");
// Basic connection settings
hibernateProps.setProperty("hibernate.hikari.dataSourceClassName",
"com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
// DataSource-specific properties for MySQL optimization
hibernateProps.setProperty("hibernate.hikari.dataSource.url",
"jdbc:mysql://localhost:3306/myapp?useSSL=false&serverTimezone=UTC");
hibernateProps.setProperty("hibernate.hikari.dataSource.user", "appuser");
hibernateProps.setProperty("hibernate.hikari.dataSource.password", "apppass");
// MySQL connection optimization
hibernateProps.setProperty("hibernate.hikari.dataSource.cachePrepStmts", "true");
hibernateProps.setProperty("hibernate.hikari.dataSource.prepStmtCacheSize", "250");
hibernateProps.setProperty("hibernate.hikari.dataSource.prepStmtCacheSqlLimit", "2048");
hibernateProps.setProperty("hibernate.hikari.dataSource.useServerPrepStmts", "true");
hibernateProps.setProperty("hibernate.hikari.dataSource.useLocalSessionState", "true");
hibernateProps.setProperty("hibernate.hikari.dataSource.rewriteBatchedStatements", "true");
hibernateProps.setProperty("hibernate.hikari.dataSource.cacheResultSetMetadata", "true");
hibernateProps.setProperty("hibernate.hikari.dataSource.cacheServerConfiguration", "true");
hibernateProps.setProperty("hibernate.hikari.dataSource.elideSetAutoCommits", "true");
hibernateProps.setProperty("hibernate.hikari.dataSource.maintainTimeStats", "false");
// Pool configuration for high-load application
hibernateProps.setProperty("hibernate.hikari.maximumPoolSize", "50");
hibernateProps.setProperty("hibernate.hikari.minimumIdle", "10");
hibernateProps.setProperty("hibernate.hikari.connectionTimeout", "20000");
hibernateProps.setProperty("hibernate.hikari.idleTimeout", "300000");
hibernateProps.setProperty("hibernate.hikari.maxLifetime", "1200000");
hibernateProps.setProperty("hibernate.hikari.poolName", "HighLoadAppPool");
hibernateProps.setProperty("hibernate.hikari.registerMbeans", "true");
hibernateProps.setProperty("hibernate.hikari.leakDetectionThreshold", "120000");HikariCP properties are prefixed with hibernate.hikari. in Hibernate configuration:
| HikariConfig Property | Hibernate Property |
|---|---|
jdbcUrl | hibernate.hikari.jdbcUrl |
username | hibernate.hikari.username |
password | hibernate.hikari.password |
maximumPoolSize | hibernate.hikari.maximumPoolSize |
minimumIdle | hibernate.hikari.minimumIdle |
connectionTimeout | hibernate.hikari.connectionTimeout |
idleTimeout | hibernate.hikari.idleTimeout |
maxLifetime | hibernate.hikari.maxLifetime |
poolName | hibernate.hikari.poolName |
registerMbeans | hibernate.hikari.registerMbeans |
DataSource properties use the hibernate.hikari.dataSource. prefix:
| DataSource Property | Hibernate Property |
|---|---|
url | hibernate.hikari.dataSource.url |
user | hibernate.hikari.dataSource.user |
password | hibernate.hikari.dataSource.password |
cachePrepStmts | hibernate.hikari.dataSource.cachePrepStmts |
import org.hibernate.SessionFactory;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import com.zaxxer.hikari.hibernate.HikariConnectionProvider;
import com.zaxxer.hikari.HikariDataSource;
// Access HikariDataSource from Hibernate SessionFactory
SessionFactory sessionFactory = // ... your session factory
ConnectionProvider connectionProvider = sessionFactory.getSessionFactoryOptions()
.getServiceRegistry()
.getService(ConnectionProvider.class);
if (connectionProvider instanceof HikariConnectionProvider) {
HikariConnectionProvider hikariProvider = (HikariConnectionProvider) connectionProvider;
// Unwrap to get HikariDataSource for monitoring
HikariDataSource dataSource = hikariProvider.unwrap(HikariDataSource.class);
// Access pool statistics via JMX or direct monitoring
String poolName = dataSource.getPoolName();
System.out.println("Hibernate using HikariCP pool: " + poolName);
}Common configuration issues and solutions:
// Issue: Connection pool not being used
// Solution: Ensure correct provider class name
hibernateProps.setProperty("hibernate.connection.provider_class",
"com.zaxxer.hikari.hibernate.HikariConnectionProvider"); // Correct
// NOT: "com.zaxxer.hikari.HikariConnectionProvider" // Incorrect
// Issue: Properties not being applied
// Solution: Use correct property prefix
hibernateProps.setProperty("hibernate.hikari.maximumPoolSize", "20"); // Correct
// NOT: "hikari.maximumPoolSize" // Incorrect
// Issue: DataSource properties ignored
// Solution: Use dataSource prefix for vendor-specific properties
hibernateProps.setProperty("hibernate.hikari.dataSource.cachePrepStmts", "true"); // Correct
// NOT: "hibernate.hikari.cachePrepStmts" // Incorrect
// Issue: Pool not shutting down properly
// Solution: Ensure proper SessionFactory lifecycle management
sessionFactory.close(); // This will trigger HikariConnectionProvider.stop()Install with Tessl CLI
npx tessl i tessl/maven-com-zaxxer--hikari-cp-java6