How to set up datasource with Spring for HikariCP?
you need to write this structure on your bean configuration (this is your datasource):
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig"> <property name="poolName" value="springHikariCP" /> <property name="connectionTestQuery" value="SELECT 1" /> <property name="dataSourceClassName" value="${hibernate.dataSourceClassName}" /> <property name="maximumPoolSize" value="${hibernate.hikari.maximumPoolSize}" /> <property name="idleTimeout" value="${hibernate.hikari.idleTimeout}" /> <property name="dataSourceProperties"> <props> <prop key="url">${dataSource.url}</prop> <prop key="user">${dataSource.username}</prop> <prop key="password">${dataSource.password}</prop> </props> </property></bean><!-- HikariCP configuration --><bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"> <constructor-arg ref="hikariConfig" /></bean>
This is my example and it is working. You just need to put your properties on hibernate.properties and set it before:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:hibernate.properties</value> </list> </property></bean>
Obs.: the versions are
log4j: 1.2.16
springframework: 3.1.4.RELEASE
HikariCP: 1.4.0
Properties file (hibernate.properties):
hibernate.dataSourceClassName=oracle.jdbc.pool.OracleDataSourcehibernate.hikari.maximumPoolSize=10hibernate.hikari.idleTimeout=30000dataSource.url=jdbc:oracle:thin:@localhost:1521:xedataSource.username=admindataSource.password=
my test java config (for MySql)
@Bean(destroyMethod = "close")public DataSource dataSource(){ HikariConfig hikariConfig = new HikariConfig(); hikariConfig.setDriverClassName("com.mysql.jdbc.Driver"); hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/spring-test"); hikariConfig.setUsername("root"); hikariConfig.setPassword("admin"); hikariConfig.setMaximumPoolSize(5); hikariConfig.setConnectionTestQuery("SELECT 1"); hikariConfig.setPoolName("springHikariCP"); hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true"); hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250"); hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048"); hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true"); HikariDataSource dataSource = new HikariDataSource(hikariConfig); return dataSource;}
You can create a datasource bean in servlet context as:
<beans:bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"> <beans:property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" /> <beans:property name="maximumPoolSize" value="5" /> <beans:property name="maxLifetime" value="30000" /> <beans:property name="idleTimeout" value="30000" /> <beans:property name="dataSourceProperties"> <beans:props> <beans:prop key="url">jdbc:mysql://localhost:3306/exampledb</beans:prop> <beans:prop key="user">root</beans:prop> <beans:prop key="password"></beans:prop> <beans:prop key="prepStmtCacheSize">250</beans:prop> <beans:prop key="prepStmtCacheSqlLimit">2048</beans:prop> <beans:prop key="cachePrepStmts">true</beans:prop> <beans:prop key="useServerPrepStmts">true</beans:prop> </beans:props> </beans:property></beans:bean>