Search code examples
hibernatejpaspring-bootc3p0

Spring Boot 1.2.5, Oracle and Hibernate connection pooling


I'm profiling my Spring Boot 1.2.5 application and finding the performance to be quite poor. Serving a simple login page takes upwards of 4 seconds under what will be a relatively light load (at this point, JMeter with 500 simulated users).

I'm using VisualVM to try to profile it. It seems that 49% of the applications time is spent getting a connection from 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

To mitigate this I am trying to enable connection pooling but it does not seem to be working. I have:

Added C3P0 to my dependancies, so my Hibernate dependancies look like this in my 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>

Also, in my application.properties file, I have added:

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

I read in the docs that if I have any Hibernate C3P0 property set then the connection pool should be active.

However, I'm not sure it is. When I start Spring Boot, some of the messages I see are:

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

The "hibernate.properties" not found is the one I am concerned with. I realize it may emit that message even if it finds the properties in application.properties.

I'm wondering, have I done something wrong and, is there a way to verify that the connection pooling is actually active?

Thanks very much...


Solution

  • With Steve Waldman's helpful comments, I got this working. For anyone interested, since I am using Spring Boot 1.2.5.RELEASE, which is based on Spring 4.1.7.RELEASE, Hibernate 5 is not easily available (although I'm working on that).

    So to make this work, put this in the 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>
    

    These properties then work from 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
    

    If you prefer, the properties can go into a hibernate.properties file, and they are a little different, like this:

    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
    

    It helped my issue, although not as much as I had hoped.