Search code examples
springhibernatespring-mvcc3p0

Hibernate c3p0 and Spring. Invalid property 'driverClassName' of bean class [org.hibernate.c3p0.internal.C3P0ConnectionProvider]


I'm currently developing an application that uses hibernate and Spring MVC. I want to implement c3p0 but I can't understand how to implement it.

I used the c3p0 jars in the optional folder, Hibernate-c3p0-5.0.2.jar and c3p0-0.9.2.1.jar.

These are my configurations.

Right now, I'm using DriverManagerDatasource from Spring.

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean> 

I tried doing this

<bean id="dataSource" class="org.hibernate.c3p0.internal.C3P0ConnectionProvider">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

    <!-- C3P0 Config -->
    <property name="hibernate.c3p0.acquire_increment" value="1" />
    <property name="hibernate.c3p0.idle_test_period" value="100" />
    <property name="hibernate.c3p0.max_size" value="10" />
    <property name="hibernate.c3p0.max_statements" value="10" />
    <property name="hibernate.c3p0.min_size" value="10" />
    <property name="hibernate.c3p0.timeout" value="100" />
</bean>

but i'm getting an error.

Invalid property 'driverClassName' of bean class [org.hibernate.c3p0.internal.C3P0ConnectionProvider]

TIA.


Solution

  • driverClassName is a DriverManagerDataSource property, not a property on the C3P0ConnectionProvider. So that's why you're getting the error.

    Instead of using the Spring DriverManagerDataSource, which is just a simple DataSource implementation and not a connection pool at all, you want to use C3P0's DataSource implementation. Try using ComboPooledDataSource. That implementation also has a driverClassName property, which you will want to set equal to your database driver (like MySQL driver or whatever).

    Here's an example that I lifted off a web page:

        cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.jdbc.Driver"); //loads the jdbc driver
        cpds.setJdbcUrl("jdbc:mysql://localhost/test");
        cpds.setUser("root");
        cpds.setPassword("root");
    

    That's not Spring, but just make the adjustment to put it in Spring. The full class name is com.mchange.v2.c3p0.ComboPooledDataSource. You can see a Spring-based example here.