简体   繁体   中英

Problems with hibernate c3p0 netbeans

I'm trying to use C3P0 as conection Provider in hbernate. I'm working with netbeans 8.0 and I added the Hibernate 4.x(JPA 2.0) Library.

This is my configuration:

public Session getOpenSession() {
Configuration pgConf = new Configuration();
pgConf.setProperty(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
pgConf.setProperty(Environment.DRIVER, "org.postgresql.Driver");
pgConf.setProperty(Environment.URL, "jdbc:postgresql://localhost:5432/testdb");
pgConf.setProperty(Environment.USER, "user");
pgConf.setProperty(Environment.PASS, "pass");
pgConf.setProperty(Environment.POOL_SIZE, "20");
pgConf.setProperty(Environment.CONNECTION_PROVIDER, "org.hibernate.connection.C3P0ConnectionProvider");
pgConf.setProperty(Environment.C3P0_MIN_SIZE, "7");
pgConf.setProperty(Environment.C3P0_MAX_SIZE, "53");
pgConf.setProperty(Environment.C3P0_TIMEOUT, "100");
pgConf.setProperty(Environment.C3P0_MAX_STATEMENTS, "50");
pgConf.setProperty(Environment.C3P0_IDLE_TEST_PERIOD, "1000");
pgConf.setProperty("hibernate.c3p0.validate", "true");

return pgConf.buildSessionFactory().openSession();
}

When I call these method it throws the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mchange/v2/log/MLog
at com.mchange.v2.c3p0.DataSources.<clinit>(DataSources.java:72)    at com.mchange.v2.c3p0.DataSources.<clinit>(DataSources.java:72)
at org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider.configure(C3P0ConnectionProvider.java:197)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:76)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:76)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1861)
at cu.uci.nimbus.Util.getPGSession(Util.java:193)
at cu.uci.nimbus.evaluacion.util.SourceProvider.<init>(SourceProvider.java:45)
at cu.uci.nimbus.evaluacion.impl.EvaluadorImpl.<init>(EvaluadorImpl.java:75)
at cu.uci.nimbus.Main.eval(Main.java:140)
at cu.uci.nimbus.Main.main(Main.java:84)
Caused by: java.lang.ClassNotFoundException: com.mchange.v2.log.MLog
at java.net.URLClassLoader$1.run(URLClassLoader.java:359)
at java.net.URLClassLoader$1.run(URLClassLoader.java:348)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:347)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 18 more

Java Result: 1

In order to solve this error, I add mchange-commons-java-0.2.8.jar wich contains com.mchange.v2.log.MLog class but still without working.

PS: Apologize me for my English.

The hibernate-core doesn't come with the C3P0 module, so you have to add the hibernate-c3p0 module to your project dependencies as well:

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

If you have it, try adding the C3P0 dependency too:

<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>

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