使用 HikariCP 配置休眠

由于c3p0连接池的问题,我想看看替代方案,并决定哪一个在我的情况下可能更有用。HikariCP看起来非常有前途,但没有文档说明如何将其与Hibernate一起使用。

到目前为止,我使用的是c3p0,如下所示:

<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>

有人可以告诉我如何以这种方式配置HikariCP吗?


答案 1

您可以使用按包装发货的。org.hibernate.hikaricp.internal.HikariCPConnectionProviderhibernate-hikaricp

您可以将其作为 Maven 依赖项安装(请不要忘记更新版本号):

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

并在hibernate.properties中配置它:

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

请注意:从Hibernate 4.3.6开始,您不应使用(请参阅:https://github.com/brettwooldridge/HikariCP/wiki/Hibernate4com.zaxxer.hikari.hibernate.HikariConnectionProvider)


答案 2

HikariCP,从版本1.2.6开始,现在通过ConnectionProvider显式支持Hibernate 4.x。有关详细信息,请参阅新的 wiki 文档


推荐