hiberate c3p0 classNotFound错误

发布于 2021-11-30 08:12:45 字数 7402 浏览 883 评论 1

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

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>



求教各位了。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤带着心事 2021-11-30 09:40:06

好吧,我自己解决了,主要问题在于你的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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文