2015-10-28 74 views
3

我正在分析我的Spring Boot 1.2.5应用程序,发现性能很差。在一个相对较轻的负载下(此时,JMeter有500个模拟用户),服务一个简单的登录页需要4秒以上。Spring Boot 1.2.5,Oracle和Hibernate连接池

我使用VisualVM尝试对其进行配置。看来的应用程序时的49%都花在正从休眠连接:

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属性设置,那么连接池应该是活动的。

但是,我不确定。当我开始春季启动,一些我看到的消息是:

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中找到属性。

我想知道,我做错了什么,有没有办法来验证连接池实际上是活动?

非常感谢...

+0

我不认为你已经做了任何事情,实际上会启用c3p0。根据您的设置,您可能需要将作为应用程序数据源的Spring bean(对c3p0的'ComboPooledDataSource')进行替换,或者将'hibernate连接提供程序类配置参数'connection.provider_class'设置为'org.hibernate.connection.C3P0ConnectionProvider'。如果设置了c3p0,库将在池初始化的INFO处记录横幅和(长)池配置消息。 –

+0

谢谢史蒂夫,但我完全没有。你是说我可以用另一个房产条目来做到这一点吗?目前,它是Spring 4.2,一切都配置了注释;没有XML。我是@Autowired JdbcTemplate,并没有提供我自己的数据源。如果我需要我可以,我只是不清楚如何。 –

+0

所以,我只是不太了解春季引导。但纯粹猜测,我会尝试'spring.jpa.properties.hibernate.connection.provider_class = org.hibernate.connection.C3P0ConnectionProvider'。同样,只要您在INFO中允许任意消息,您应该能够在日志中看到c3p0启动。 (否则,您可能需要配置以'com.mchange'库为前缀的记录器,通过您使用的任何记录库来登录INFO。) –

回答

2

随着史蒂夫沃尔德曼的有益的评论,我得到了这个工作。对于任何感兴趣的人,因为我使用基于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 

如果你愿意的话,属性可以进入休眠.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 

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