简体   繁体   中英

can't create dataSource bin with Spring, Hibernate and c3p0

I can't create dataSource bin as c3p0 connectionPool with Spring, Hibernate and MySql

My errors:

java.lang.NoSuchMethodError: com.mchange.v2.c3p0.impl.C3P0Defaults.dataSourceName()Ljava/lang/String;
    at com.mchange.v2.c3p0.impl.PoolBackedDataSourceBase.<init>(PoolBackedDataSourceBase.java:49)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.<init>(AbstractPoolBackedDataSource.java:74)
    at com.mchange.v2.c3p0.ComboPooledDataSource.<init>(ComboPooledDataSource.java:134)
    at com.mchange.v2.c3p0.ComboPooledDataSource.<init>(ComboPooledDataSource.java:130)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.mchange.v2.c3p0.ComboPooledDataSource]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: com.mchange.v2.c3p0.impl.C3P0Defaults.dataSourceName()Ljava/lang/String;

My spring config:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/screenshots" />
    <property name="user" value="root" />
    <property name="password" value="root" />

    <property name="acquireIncrement" value="1"></property>
    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="10" />
    <property name="maxIdleTime" value="1800" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" lazy-init="false" autowire="default">
    <property name="dataSource">
        <ref local="dataSource" />
    </property>
    <property name="packagesToScan" value="com.webscreenshots.bean"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

and my POM dependencies:

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.28</version>
        </dependency>

        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>4.3.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.0.Final</version>
        </dependency>

I am using Spring 3.2.

This is typican configuration and I have no mind what's going wrong. Could you give me some advices, please?

It's very likely that you have an old version of c3p0 buried somewhere in your application's CLASSPATH, in a way that, combined with ClassLoader delegation, has led to mixed versions of c3p0 in your effective codebase. The method whose call triggers the NoSuchMethod error was added somewhere around 0.9.2-pre6 ( well prior to the version you think you are using, c3p0 0.9.2.1). Maybe you have brought in a 0.9.1.x version along with your managed dependencies (note that versions earlier than c3p0-0.9.2.x have a different group name than more recent versions), or else you have an unmanaged jar file lying around somewhere.

The dependencies that you list above look okay, hibernate-c3p0-4.3.0.Final depends on the same version of c3p0 that you have added explicitly. But you might want to try not adding c3p0 explicitly as a dependency and just let hibernate-c3p0 bring in its version. Note that the version of c3p0 you want was made a hibernate dependency as of 4.2.3; if somehow you are bringing in dependencies from versions older than that you may have problems. One way or another, you need to understand your app's effective CLASSPATH (including JVM extensions directories) and figure out where an old version coexists with the version you mean to be using.

I thing the issue is with your acquireIncrement property of com.mchange.v2.c3p0.ComboPooledDataSource class it should be like

<property name="acquireIncrement">1</property>

OR

<property name="acquireIncrement" value="1"/>

try to change this and run your application again

Configuring the built-in c3p0 pooling in Hibernate using Spring

hope this will solve your problem !

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