1
回答
hiberate c3p0 classNotFound错误
华为云实践训练营,热门技术免费实践!>>>   

这个奇怪的问题困扰我两天了

Caused by: java.lang.NoClassDefFoundError: org/hibernate/engine/jdbc/connections/spi/ConnectionProvider
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2888)
	at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1172)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
	at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:313)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:270)
	at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:149)
	at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:189)
	at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:114)
	at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:54)
	at org.hibernate.service.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:70)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:177)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:151)
	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 org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
	at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
	at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
	... 34 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.jdbc.connections.spi.ConnectionProvider
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)



再来看看我的hibernate配置文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- 配置c3p0连接池(解决MySQL空闲8小时自动断开问题) -->
		<property name="hibernate.connection.provider_class">
		org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
		<property name="hibernate.c3p0.acquireIncrement">2</property>
		<property name="hibernate.c3p0.checkoutTimeout">3000</property>
		<property name="hibernate.c3p0.idleConnectionTestPeriod">2000</property>
		<property name="hibernate.c3p0.maxIdleTime">4000</property>
		<property name="hibernate.c3p0.maxPoolSize">60</property>
		<property name="hibernate.c3p0.minPoolSize">1</property>
		<property name="hibernate.c3p0.maxStatements">0</property>

		<property name="hibernate.c3p0.validate">true</property>
		<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
		<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="hibernate.connection.url">
			jdbc:mysql://localhost:3306/picpoll?autoReconnect=true&amp;autoReconnectForPools=true&amp;useunicode=true&amp;characterEncoding=utf8</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
		<property name="hibernate.search.autoregister_listeners">false</property>
		<mapping resource="com/pwc/hibernate/Poll.hbm.xml" />
		<mapping resource="com/pwc/hibernate/Pic.hbm.xml" />


	</session-factory>
</hibernate-configuration>



其实最开始的时候,connection.provider_class是这样的,但是马上就报了C3P0ConnectionProvider类找不到的错误。

<!-- 配置c3p0连接池(解决MySQL空闲8小时自动断开问题) -->
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>



后来我才改成刚才文件中贴的那样

<!-- 配置c3p0连接池(解决MySQL空闲8小时自动断开问题) -->
		<property name="hibernate.connection.provider_class">
		org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>



目的就是为了解决mysql8小时断开的错误。

另外我用的pom是

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



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



求教各位了。

举报
共有1个答案 最后回答: 2年前

好吧,我自己解决了,主要问题在于你的c3p0 hibernate包必须与hibernate core的版本一致

<!-- hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		
		<!-- Hibernate c3p0 connection pool -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-c3p0</artifactId>
			<version>${hibernate.version}</version>
		</dependency>



顶部