Java 使用 HikariCP 配置 Hibernate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20899455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:07:00  来源:igfitidea点击:

Configuring Hibernate with HikariCP

javahibernatedatabase-connectionc3p0hikaricp

提问by Vojtěch

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.

由于 c3p0 连接池的问题,我想查看替代方案并决定哪一个在我的情况下可能更有用。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吗?

采纳答案by brettw

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

HikariCP,为1.2.6版本,现在支持休眠4.x中明确将其ConnectionProvider来。有关详细信息,请参阅新的wiki 文档

回答by brettw

UPDATE: See uwolfer's answer below, it is now the official way to use HikariCP with Hibernate.

更新:请参阅下面 uwolfer 的回答,它现在是在 Hibernate 中使用 HikariCP 的官方方法。

I'm one of the authors of 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:

我是 HikariCP 的作者之一。我并不声称自己是 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 BoneCPas the mainDataSource, simply try replacing that section with configuration for HikariCPinstead.

在该页面的 XML 配置部分中,他们的示例使用BoneCP作为mainDataSource,只需尝试用HikariCP 的配置替换该部分即可

In your example above, you appear to be configuring Hibernate through Spring and defining the DataSourceinside of the Hibernate config, which is fine. But an alternative (presented on that page) is to configure the DataSourceseparately through Spring and then directing Hibernate to use it.

在上面的示例中,您似乎是通过 Spring 配置 Hibernate 并定义DataSourceHibernate 配置的内部,这很好。但是另一种方法(在该页面上显示)是DataSource通过 Spring 单独配置它,然后指示 Hibernate 使用它。

Regarding statement caching, HikariCPdoes not do it because we believe that is best left to the vendors' JDBC driver/DataSource. Almost every major DB vendors' JDBC DataSourceprovides statement caching, and it can be configured throughHikariCP by specifying DataSourceproperties. Refer to the HikariCPgithub page for how to set properties on the underlying (vendor) DataSource.

关于语句缓存,HikariCP不这样做,因为我们认为最好留给供应商的 JDBC 驱动程序/数据源。几乎各大DB厂商的JDBC都DataSource提供了语句缓存,可以通过HikariCP通过指定DataSource属性进行配置。有关如何在底层(供应商)上设置属性,请参阅HikariCPgithub 页面DataSource

回答by uwolfer

You can use the org.hibernate.hikaricp.internal.HikariCPConnectionProviderwhich is shipped by hibernate-hikaricppackage.

您可以使用org.hibernate.hikaricp.internal.HikariCPConnectionProvider通过hibernate-hikaricp包装运送的。

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