Ultimate JDBC Connection Pool
—
Native integration with popular Java frameworks including Hibernate ORM and JNDI environments for seamless adoption in enterprise applications.
Native integration with Hibernate ORM providing connection pooling for Hibernate-based applications.
/**
* Connection provider for Hibernate 4.3+. This provider is deprecated in favor of
* the official Hibernate HikariCP provider available in Hibernate 4.3.6+.
*/
public class HikariConnectionProvider implements ConnectionProvider, Configurable, Stoppable {
/**
* Default constructor
*/
public HikariConnectionProvider();
}ConnectionProvider Methods:
/**
* Get a connection from the HikariCP pool
* @return Connection from the pool
* @throws SQLException if unable to obtain connection
*/
public Connection getConnection() throws SQLException;
/**
* Close/return a connection to the pool
* @param conn the connection to close
* @throws SQLException if error occurs closing connection
*/
public void closeConnection(Connection conn) throws SQLException;
/**
* HikariCP does not support aggressive connection release
* @return false - aggressive release not supported
*/
public boolean supportsAggressiveRelease();
/**
* Check if this provider can be unwrapped to the specified type
* @param unwrapType the type to check for unwrapping
* @return true if unwrappable to the specified type
*/
public boolean isUnwrappableAs(Class unwrapType);
/**
* Unwrap this provider to the specified type
* @param unwrapType the type to unwrap to
* @return unwrapped object
* @throws UnknownUnwrapTypeException if unwrapping is not supported
*/
public <T> T unwrap(Class<T> unwrapType);Configuration Methods:
/**
* Configure the connection provider from Hibernate properties
* @param props Map of Hibernate configuration properties
* @throws HibernateException if configuration fails
*/
public void configure(Map props) throws HibernateException;
/**
* Stop the connection provider and close the data source
*/
public void stop();Usage Examples:
// Hibernate configuration (hibernate.cfg.xml)
/*
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.provider_class">
com.zaxxer.hikari.hibernate.HikariConnectionProvider
</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/mydb</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<!-- HikariCP specific settings -->
<property name="hibernate.hikari.maximumPoolSize">20</property>
<property name="hibernate.hikari.minimumIdle">5</property>
<property name="hibernate.hikari.connectionTimeout">30000</property>
<property name="hibernate.hikari.idleTimeout">600000</property>
<property name="hibernate.hikari.maxLifetime">1800000</property>
<property name="hibernate.hikari.leakDetectionThreshold">60000</property>
<!-- DataSource properties -->
<property name="hibernate.hikari.dataSource.cachePrepStmts">true</property>
<property name="hibernate.hikari.dataSource.prepStmtCacheSize">250</property>
<property name="hibernate.hikari.dataSource.prepStmtCacheSqlLimit">2048</property>
</session-factory>
</hibernate-configuration>
*/
// Programmatic configuration
Properties hibernateProps = new Properties();
hibernateProps.setProperty("hibernate.connection.provider_class",
"com.zaxxer.hikari.hibernate.HikariConnectionProvider");
hibernateProps.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
hibernateProps.setProperty("hibernate.connection.url", "jdbc:postgresql://localhost:5432/mydb");
hibernateProps.setProperty("hibernate.connection.username", "user");
hibernateProps.setProperty("hibernate.connection.password", "password");
// HikariCP settings
hibernateProps.setProperty("hibernate.hikari.maximumPoolSize", "20");
hibernateProps.setProperty("hibernate.hikari.minimumIdle", "5");
hibernateProps.setProperty("hibernate.hikari.connectionTimeout", "30000");
Configuration configuration = new Configuration();
configuration.setProperties(hibernateProps);
SessionFactory sessionFactory = configuration.buildSessionFactory();Utility class for mapping Hibernate properties to HikariCP configuration.
/**
* Utility class to map Hibernate properties to HikariCP configuration properties
*/
public class HikariConfigurationUtil {
/** Configuration prefix for HikariCP properties in Hibernate */
public static final String CONFIG_PREFIX = "hibernate.hikari.";
/** Configuration prefix for DataSource properties in Hibernate */
public static final String CONFIG_PREFIX_DATASOURCE = "hibernate.hikari.dataSource.";
/**
* Create/load a HikariConfig from Hibernate properties
* @param props a map of Hibernate properties
* @return a HikariConfig instance
*/
public static HikariConfig loadConfiguration(Map props);
}Usage Examples:
// Custom Hibernate configuration loading
Map<String, String> hibernateProperties = new HashMap<>();
hibernateProperties.put("hibernate.connection.driver_class", "org.postgresql.Driver");
hibernateProperties.put("hibernate.connection.url", "jdbc:postgresql://localhost:5432/mydb");
hibernateProperties.put("hibernate.connection.username", "user");
hibernateProperties.put("hibernate.connection.password", "password");
// HikariCP specific properties
hibernateProperties.put("hibernate.hikari.maximumPoolSize", "25");
hibernateProperties.put("hibernate.hikari.minimumIdle", "5");
hibernateProperties.put("hibernate.hikari.connectionTimeout", "20000");
// DataSource specific properties
hibernateProperties.put("hibernate.hikari.dataSource.cachePrepStmts", "true");
hibernateProperties.put("hibernate.hikari.dataSource.prepStmtCacheSize", "250");
// Load HikariConfig from Hibernate properties
HikariConfig hikariConfig = HikariConfigurationUtil.loadConfiguration(hibernateProperties);
// Use with HikariDataSource directly
HikariDataSource dataSource = new HikariDataSource(hikariConfig);Standard Hibernate properties are automatically mapped to HikariCP equivalents:
| Hibernate Property | HikariCP Property |
|---|---|
hibernate.connection.isolation | transactionIsolation |
hibernate.connection.autocommit | autoCommit |
hibernate.connection.driver_class | driverClassName |
hibernate.connection.url | jdbcUrl |
hibernate.connection.username | username |
hibernate.connection.password | password |
Factory class for creating HikariDataSource instances through JNDI lookup.
/**
* A JNDI factory that produces HikariDataSource instances
*/
public class HikariJNDIFactory implements ObjectFactory {
/**
* Create a HikariDataSource instance from JNDI reference
* @param obj the JNDI reference object
* @param name the JNDI name
* @param nameCtx the naming context
* @param environment the environment hashtable
* @return HikariDataSource instance or null if not applicable
* @throws Exception if creation fails
*/
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable<?, ?> environment) throws Exception;
}Usage Examples:
// JNDI Resource configuration (web.xml or context.xml)
/*
<Resource name="jdbc/MyDB"
auth="Container"
type="javax.sql.DataSource"
factory="com.zaxxer.hikari.HikariJNDIFactory"
jdbcUrl="jdbc:postgresql://localhost:5432/mydb"
username="user"
password="password"
maximumPoolSize="20"
minimumIdle="5"
connectionTimeout="30000"
idleTimeout="600000"
maxLifetime="1800000" />
*/
// Application code using JNDI lookup
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource dataSource = (DataSource) envContext.lookup("jdbc/MyDB");
// Use DataSource normally
try (Connection conn = dataSource.getConnection()) {
// Database operations
}
// Alternative direct lookup
DataSource dataSource = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/MyDB");Configure HikariCP to wrap an existing JNDI DataSource:
Usage Examples:
// JNDI configuration with DataSource reference
/*
<Resource name="jdbc/MyDB"
auth="Container"
type="javax.sql.DataSource"
factory="com.zaxxer.hikari.HikariJNDIFactory"
dataSourceJNDI="java:/comp/env/jdbc/UnderlyingDataSource"
maximumPoolSize="20"
minimumIdle="5"
connectionTimeout="30000" />
<Resource name="jdbc/UnderlyingDataSource"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/mydb"
username="user"
password="password" />
*/
// This configuration will:
// 1. Look up the underlying DataSource from JNDI
// 2. Wrap it with HikariCP connection pooling
// 3. Make the pooled DataSource available via JNDIWhile HikariCP doesn't provide Spring-specific classes, it integrates seamlessly with Spring's DataSource configuration.
Usage Examples:
// Spring Boot application.properties
/*
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.jdbc-url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.hikari.username=user
spring.datasource.hikari.password=password
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.pool-name=MyAppPool
*/
// Spring XML configuration
/*
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/mydb"/>
<property name="username" value="user"/>
<property name="password" value="password"/>
<property name="maximumPoolSize" value="20"/>
<property name="minimumIdle" value="5"/>
<property name="connectionTimeout" value="30000"/>
<property name="idleTimeout" value="600000"/>
<property name="maxLifetime" value="1800000"/>
</bean>
*/
// Spring Java Configuration
@Configuration
public class DatabaseConfig {
@Bean(destroyMethod = "close")
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost:5432/mydb");
config.setUsername("user");
config.setPassword("password");
config.setMaximumPoolSize(20);
config.setMinimumIdle(5);
config.setConnectionTimeout(30000);
config.setIdleTimeout(600000);
config.setMaxLifetime(1800000);
config.setPoolName("MyAppPool");
return new HikariDataSource(config);
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
// Spring Boot Configuration Class
@Configuration
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public class HikariConfigProperties extends HikariConfig {
// Spring Boot will automatically populate this from application.properties
}
@Configuration
public class DataSourceConfig {
@Bean
@Primary
public DataSource dataSource(HikariConfigProperties hikariConfig) {
return new HikariDataSource(hikariConfig);
}
}Integration patterns for various application servers.
Usage Examples:
// Tomcat context.xml
/*
<Context>
<Resource name="jdbc/MyDB"
auth="Container"
type="javax.sql.DataSource"
factory="com.zaxxer.hikari.HikariJNDIFactory"
jdbcUrl="jdbc:postgresql://localhost:5432/mydb"
username="user"
password="password"
maximumPoolSize="20" />
</Context>
*/
// WildFly/JBoss standalone.xml datasource
/*
<datasource jndi-name="java:/jdbc/MyDB" pool-name="MyDBPool">
<connection-url>jdbc:postgresql://localhost:5432/mydb</connection-url>
<driver>postgresql</driver>
<security>
<user-name>user</user-name>
<password>password</password>
</security>
<pool>
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
</pool>
</datasource>
*/
// WebLogic data source configuration (programmatic)
public class WebLogicDataSourceConfig {
public DataSource createHikariDataSource() throws NamingException {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost:5432/mydb");
config.setUsername("user");
config.setPassword("password");
config.setMaximumPoolSize(20);
config.setMinimumIdle(5);
HikariDataSource dataSource = new HikariDataSource(config);
// Bind to JNDI for application access
Context context = new InitialContext();
context.bind("java:comp/env/jdbc/MyDB", dataSource);
return dataSource;
}
}Patterns for enterprise application integration with connection pooling.
Usage Examples:
// Enterprise application lifecycle management
public class ApplicationDataSourceManager {
private HikariDataSource dataSource;
private HikariPoolMXBean poolMBean;
@PostConstruct
public void initialize() {
// Load configuration from enterprise configuration system
HikariConfig config = loadConfigFromEnterprise();
// Enable JMX for monitoring
config.setRegisterMbeans(true);
config.setPoolName("EnterpriseAppPool");
// Create data source
dataSource = new HikariDataSource(config);
poolMBean = dataSource.getHikariPoolMXBean();
// Register with enterprise monitoring
registerWithMonitoringSystem();
}
@PreDestroy
public void shutdown() {
if (dataSource != null && !dataSource.isClosed()) {
// Graceful shutdown
poolMBean.suspendPool();
// Wait for active connections to finish
int attempts = 0;
while (poolMBean.getActiveConnections() > 0 && attempts < 30) {
try {
Thread.sleep(1000);
attempts++;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
dataSource.close();
}
}
public DataSource getDataSource() {
return dataSource;
}
private HikariConfig loadConfigFromEnterprise() {
// Load from enterprise configuration system
// This could be from LDAP, database, config server, etc.
return new HikariConfig();
}
private void registerWithMonitoringSystem() {
// Register with enterprise monitoring system
// This could be with JMX, custom monitoring, etc.
}
}Install with Tessl CLI
npx tessl i tessl/maven-com-zaxxer--hikari-cp-java7