【问题标题】:Spring Boot 1.2.5, Oracle and Hibernate connection poolingSpring Boot 1.2.5、Oracle 和 Hibernate 连接池
【发布时间】:2016-01-27 22:43:55
【问题描述】:

我正在分析我的 Spring Boot 1.2.5 应用程序并发现性能很差。在相对较轻的负载下提供一个简单的登录页面需要 4 秒以上(此时,具有 500 个模拟用户的 JMeter)。

我正在使用 VisualVM 来尝试对其进行分析。似乎 49% 的应用程序时间都花在了从 Hibernate 获取连接上:

 org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection()    49.121124   4,450,911 ms (49.1%)    0.000 ms    4,573,860 ms    122,949 ms

为了缓解这种情况,我尝试启用连接池,但它似乎不起作用。我有:

将 C3P0 添加到我的依赖项中,因此我的 Hibernate 依赖项在我的 pom 中如下所示:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>4.2.3.Final</version>
    </dependency>

另外,在我的 application.properties 文件中,我添加了:

spring.jpa.properties.hibernate.c3p0.min_size = 50
spring.jpa.properties.hibernate.c3p0.timeout = 300

我在文档中读到,如果我设置了任何 Hibernate C3P0 属性,那么连接池应该是活动的。

但是,我不确定。当我启动 Spring Boot 时,我看到的一些消息是:

2015-10-28 04:26:23.426  INFO 2182 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.3.Final}
2015-10-28 04:26:23.429  INFO 2182 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2015-10-28 04:26:23.431  INFO 2182 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2015-10-28 04:26:23.756  INFO 2182 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2015-10-28 04:26:24.207  INFO 2182 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect

未找到的“hibernate.properties”是我所关心的。我意识到即使它在 application.properties 中找到属性,它也可能会发出该消息。

我想知道,我做错了什么吗?有没有办法验证连接池是否真的处于活动状态?

非常感谢...

【问题讨论】:

  • 我认为您没有做过任何可以真正启用 c3p0 的操作。根据您的设置,您可能需要替换作为应用程序数据源的 Spring bean(到 c3p0 的 ComboPooledDataSource)或将休眠连接提供程序类配置参数 connection.provider_class 设置为 org.hibernate.connection.C3P0ConnectionProvider。如果设置了 c3p0,则库将在池初始化时在 INFO 中记录横幅和(长)池配置消息。
  • 感谢史蒂夫,但我不完全遵循。你是说我可以只用另一个属性条目来做到这一点?目前是 Spring 4.2,一切都配置了注解;没有 xml。我是@Autowired JdbcTemplate,并没有提供我自己的数据源。如果需要,我可以,我只是不清楚如何。
  • 所以,我只是不太了解 spring-boot。但纯粹是猜测,我会尝试spring.jpa.properties.hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider。同样,只要您在 INFO 允许任意消息通过,您应该能够在日志中看到 c3p0 启动。 (否则您可能需要配置以com.mchange libs 为前缀的记录器,以通过您使用的任何日志库登录 INFO。)
  • 再次感谢史蒂夫,我认为部分问题在于我使用的版本不同。当我包含您推荐的属性时,有一个找不到类的异常。我正在尝试将我的 Hibernate 全部更新到最新版本(5.x)并找出当前正确的类名是什么。另外,我可能需要对 c3p0 的依赖。不知道这是否会有所帮助,但一旦下载我就会发现。

标签: hibernate jpa spring-boot c3p0


【解决方案1】:

借助 Steve Waldman 的有用 cmets,我得到了这个工作。对于任何感兴趣的人,由于我使用的是基于 Spring 4.1.7.RELEASE 的 Spring Boot 1.2.5.RELEASE,Hibernate 5 不容易获得(尽管我正在努力)。

所以要让这个工作,把它放在 pom 中:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.3.Final</version>
</dependency>           
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>4.3.11.Final</version>
</dependency>

这些属性然后在 application.properties 中起作用:

spring.jpa.properties.hibernate.c3p0.max_size 2000
spring.jpa.properties.hibernate.c3p0.min_size 100
spring.jpa.properties.hibernate.c3p0.timeout 5000
spring.jpa.properties.hibernate.c3p0.max_statements 1000
spring.jpa.properties.hibernate.c3p0.idle_test_period 3000
spring.jpa.properties.hibernate.c3p0.acquire_increment 2
spring.jpa.properties.hibernate.c3p0.validate false

spring.jpa.properties.hibernate.connection.provider_class = org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider
spring.jpa.properties.hibernate.connection.url=jdbc:oracle:thin:@earth-db-11.mit.edu:1521:stardev

spring.jpa.properties.hibernate.connection.username=yourun
spring.jpa.properties.hibernate.connection.password=yourpw
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

如果您愿意,可以将这些属性放入 hibernate.properties 文件中,它们有点不同,如下所示:

hibernate.c3p0.max_size 2000
hibernate.c3p0.min_size 100
hibernate.c3p0.timeout 5000
hibernate.c3p0.max_statements 1000
hibernate.c3p0.idle_test_period 3000
hibernate.c3p0.acquire_increment 2
hibernate.c3p0.validate false

hibernate.connection.provider_class = org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider
hibernate.connection.url=jdbc:oracle:thin:@earth-db-11.mit.edu:1521:stardev

hibernate.connection.username=yourun
hibernate.connection.password=yourpw
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

它对我的问题有所帮助,尽管没有我希望的那么多。

【讨论】:

    猜你喜欢
    • 2016-12-10
    • 2022-06-29
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2015-10-05
    • 2018-04-16
    相关资源
    最近更新 更多