简体   繁体   中英

c3p0 pooling is not working (with hibernate and a datasource, on tomcat)

We have a java web application running on tomcat 6, which is calling a jar for persistance. This jar is located in the tomcat lib folder, and uses a tomcat datasource :

<Resource name="jdbc/mydatasource"
          auth="Container"
          type="javax.sql.DataSource"
          username="USER"
          password="PASSWORD"
          driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://HOST:5432/DATABASE"/>

The jar is the binary of a java project, which uses hibernate 3.5.6-Final for ORM. The hibernate configuration is the following :

    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">1800</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.connection.datasource">java:jdbc/mydatasource</property> 
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> 

At first, we did not include the hibernate-c3p0 bundle in the application, so the c3p0 settings were not used. And the whole application works fine like this.

We added the hibernate-c3p0 bundle, in order to enable a better pooling management. So this bundle is now embedded in the jar for persistance.

But we have now this error (it appears after each startup, when the weba pplication tries to retrieve data from the persistance jar) :

[Task-Thread-for-com.mchange.v2.async.ThreadPerTaskAsynchronousRunner@eafa5f] 13 :46:22 WARN - BasicResourcePool$AcquireTask: () com.mchange.v2.resourcepool.Basi cResourcePool$AcquireTask@b321c1 -- Acquisition Attempt Failed!!! Clearing pendi ng acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisi tion attempt exception: java.lang.NullPointerException at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507) at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476) at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307) at java.sql.DriverManager.getDriver(DriverManager.java:253) at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataS ource.java:224) at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManag erDataSource.java:120) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnecti on(WrapperConnectionPoolDataSource.java:143) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnecti on(WrapperConnectionPoolDataSource.java:132) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionRe sourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137) at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResource Pool.java:1014) at com.mchange.v2 .resourcepool.BasicResourcePool.access$800(BasicResourc ePool.java:32) at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicRe sourcePool.java:1810) at com.mchange.v2.async.ThreadPerTaskAsynchronousRunner$TaskThread.run(T hreadPerTaskAsynchronousRunner.java:255)

This seems wrong because the Ojdbc driver seems to be used instead of the postgresql driver defined in the datasource.

Do you know what can make this error appear, and how to solve it ?

I tried different things in the hibernate configuration, that i found in stackoverflow and different forums, which do not solve the problem :

<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="driverClass">org.postgresql.Driver</property>
    <property name="hibernate.c3p0.driverClass">org.postgresql.Driver</property>

I must precise that we only have under our control the jar used for persistance and the tomcat installation. We cannot modify the web application which is calling the jar.

Maybe your app is not be going to a Tomcat configured datasource at all, is just working through hibernate? Have you tried skipping the Resource XML (which isn't quite right for a c3p0 DataSource anyway), and just configuring hibernate for c3p0?

<!-- add these -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://HOST:5432/DATABASE</property>
<property name="hibernate.connection.username">USER</property>
<property name="hibernate.connection.password">PASSWORD</property>

<!-- same as before -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> 

<!-- REMOVE THIS! -->
<!-- <property name="hibernate.connection.datasource">java:jdbc/mydatasource</property> --> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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