简体   繁体   English

使用HikariCP配置Hibernate

[英]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. 由于c3p0连接池的问题,我想看看替代方案,并决定哪一个可能更适用于我的情况。 HikariCP looks very promising, but there is no documentation how to use it with Hibernate. HikariCP看起来非常有前景,但没有文档说明如何将它与Hibernate一起使用。

So far I am using c3p0 as follows: 到目前为止,我使用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>

Can someone point me how to configure HikariCP in such way? 有人能指出我如何以这种方式配置HikariCP吗?

You can use the org.hibernate.hikaricp.internal.HikariCPConnectionProvider which is shipped by hibernate-hikaricp package. 您可以使用hibernate-hikaricp软件包附带的org.hibernate.hikaricp.internal.HikariCPConnectionProvider

You can install it as Maven dependency (please don't forget to update the version number): 您可以将其安装为Maven依赖项(请不要忘记更新版本号):

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

And configure it in hibernate.properties: 并在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 ) 请注意:从Hibernate 4.3.6开始,你不应使用com.zaxxer.hikari.hibernate.HikariConnectionProvider (参见: https//github.com/brettwooldridge/HikariCP/wiki/Hibernate4

HikariCP , as of version 1.2.6, now supports Hibernate 4.x explicitly with a ConnectionProvider . 从版本1.2.6开始, HikariCP现在通过ConnectionProvider显式支持Hibernate 4.x. See the new wiki documentation for details. 有关详细信息,请参阅新的wiki文档

UPDATE: See uwolfer's answer below, it is now the official way to use HikariCP with Hibernate. 更新:请参阅下面的uwolfer答案,现在是将HikariCP与Hibernate一起使用的官方方式。

I'm one of the authors of HikariCP. 我是HikariCP的作者之一。 I don't claim to be a Spring guy, and I weened off of Hibernate a few years back, but this link might be helpful: 我并不认为自己是一个Spring人,几年前我就离开了Hibernate,但这个链接可能会有所帮助:

http://www.luckyryan.com/2013/02/20/spring-mvc-with-basic-persistence-spring-data-jpa-hibernate/ http://www.luckyryan.com/2013/02/20/spring-mvc-with-basic-persistence-spring-data-jpa-hibernate/

In the XML configuration section on that page, where their example uses BoneCP as the mainDataSource , simply try replacing that section with configuration for HikariCP instead. 在该页面,在那里他们的例子使用BoneCP作为mainDataSource上的XML配置部分,只是尝试配置替换该节HikariCP代替。

In your example above, you appear to be configuring Hibernate through Spring and defining the DataSource inside of the Hibernate config, which is fine. 在上面的示例中,您似乎是通过Spring配置Hibernate并在Hibernate配置中定义DataSource ,这很好。 But an alternative (presented on that page) is to configure the DataSource separately through Spring and then directing Hibernate to use it. 但另一种选择(在该页面上显示)是通过Spring单独配置DataSource ,然后指示Hibernate使用它。

Regarding statement caching, HikariCP does not do it because we believe that is best left to the vendors' JDBC driver/DataSource. 关于语句缓存, HikariCP没有这样做,因为我们认为最好留给供应商的JDBC驱动程序/ DataSource。 Almost every major DB vendors' JDBC DataSource provides statement caching, and it can be configured through HikariCP by specifying DataSource properties. 几乎每个主要数据库供应商的JDBC DataSource提供语句缓存,并且可以通过指定DataSource属性通过 HikariCP进行配置。 Refer to the HikariCP github page for how to set properties on the underlying (vendor) DataSource . 有关如何在底层(供应商) DataSource上设置属性,请参阅HikariCP github页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM