NoClassDefFoundError:com / mchange / v2 / c3p0 / DataSources

时间:2014-09-10 23:53:00

标签: java hibernate maven

我正在使用Hibernate c3p0的连接池

<property name="hibernate.c3p0.acquire_increment">1</property>
        <property name="hibernate.c3p0.idle_test_period">100</property>
        <property name="hibernate.c3p0.max_size">10</property>
        <property name="hibernate.c3p0.max_statements">10</property>
        <property name="hibernate.c3p0.min_size">10</property>
        <property name="hibernate.c3p0.timeout">100</property>
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

依赖关系在我的pom.xml中定义,jar在库中定义

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

启动应用程序时,C3P0连接器被拾取:

INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.connection.C3P0ConnectionProvider
Sep 10, 2014 7:39:39 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: net.sourceforge.jtds.jdbc.Driver at URL: jdbc:jtds:sqlserver://localhost:1433/myDatabase
Sep 10, 2014 7:39:39 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure

但是,代码在第21行中断:

3 | import org.hibernate.Session;
4 | import org.hibernate.SessionFactory;
5 | import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
6 | import org.hibernate.cfg.Configuration;
...
...
19 | Configuration configuration = new Configuration().configure();
20 | StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
21 | SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());

然后返回:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/DataSources
    at org.hibernate.c3p0.internal.C3P0ConnectionProvider.configure(C3P0ConnectionProvider.java:203)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at com.boa.ecris.test.Main.main(Main.java:21)
Caused by: java.lang.ClassNotFoundException: com.mchange.v2.c3p0.DataSources
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

我一直在网上搜索已弃用的罐子等等。但是还没有运气。 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

引起:java.lang.ClassNotFoundException:com.mchange.v2.c3p0.DataSources

错误本身表示无法从添加到 Maven 依赖项中的 jar 中找到 com.mchange.v2.c3p0.DataSources 类。 当您配置 Hibernate 时抛出异常,即, 在第 19 行:配置配置 = new Configuration().configure();

请将 c3p0-0.9.1.2.jar 添加到类路径或通过链接 maven dependency 在 pom.xml 中添加依赖项。 就我而言,它奏效了!!希望对你也有帮助...