Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Hibernate with HikariCP

Because of problems with c3p0 connection pool, I want to see the alternatives and decide which one might be more usable in my case. HikariCP looks very promising, but there is no documentation how to use it with Hibernate.

So far I am using c3p0 as follows:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">${database.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">${database.structure}</prop>
            <prop key="hibernate.connection.url">${database.connection}</prop>
            <prop key="hibernate.connection.username">${database.username}</prop>
            <prop key="hibernate.connection.password">${database.password}</prop>
            <prop key="hibernate.connection.driver_class">${database.driver}</prop>
            <prop key="hibernate.connection.shutdown">true</prop>
            <prop key="hibernate.connection.writedelay">0</prop>
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
            <prop key="hibernate.show_sql">${database.show_sql}</prop>
            <prop key="hibernate.format_sql">false</prop>
            <prop key="hibernate.ejb.metamodel.generation">disabled</prop>
            <!-- Use the C3P0 connection pool provider -->
            <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
            <prop key="hibernate.c3p0.min_size">5</prop>
            <prop key="hibernate.c3p0.max_size">30</prop>
            <prop key="hibernate.c3p0.timeout">300</prop>
            <prop key="hibernate.c3p0.max_statements">50</prop>
            <prop key="hibernate.c3p0.idle_test_period">600</prop>
        </props>
    </property>

Can someone point me how to configure HikariCP in such way?

like image 214
Vojtěch Avatar asked Jan 03 '14 08:01

Vojtěch


2 Answers

You can use the org.hibernate.hikaricp.internal.HikariCPConnectionProvider which is shipped by hibernate-hikaricp package.

You can install it as Maven dependency (please don't forget to update the version number):

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-hikaricp</artifactId>
    <version>5.2.10.Final</version>
</dependency>

And configure it in hibernate.properties:

`hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider`

Please note: As of Hibernate 4.3.6 you should no longer use com.zaxxer.hikari.hibernate.HikariConnectionProvider (see: https://github.com/brettwooldridge/HikariCP/wiki/Hibernate4)

like image 193
uwolfer Avatar answered Sep 28 '22 00:09

uwolfer


HikariCP, as of version 1.2.6, now supports Hibernate 4.x explicitly with a ConnectionProvider. See the new wiki documentation for details.

like image 26
brettw Avatar answered Sep 28 '22 00:09

brettw