发布于 2015-11-13 11:29:09 | 951 次阅读 | 评论: 1 | 来源: PHPERZ
Hibernate 开源对象关系映射框架 ORM
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。
在Hibernate里配置proxool数据库连接池时出现了标题中的报错信息
Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
...
Stacktrace:] with root cause
org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.connection.ProxoolConnectionProvider] as strategy [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:113)
at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:191)
at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:111)
at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:41)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
...
出现该报错后,百度之后未能解决问题,因此记录下自己的解决方案
二 问题分析:
根据出错信息提示,主要是找不到“org.hibernate.connection.ProxoolConnectionProvider”这个Class文件,而这个类是在hibernate.cfg.xml中使用到的
于是,我就在Hibernate的依赖包里仔细看了下,确实没有这个类
到这里,原因已经很明显了,那就是:
在最新版的Hibernate(hibernate-release-5.0.2.Final)里,lib/required里已经没有集成第三方的数据库连接池了,因此会出现类找到不到的情况。
方案1:
将hibernate-core-5.0.2.Final.jar删掉,并且添加:hibernate3.jar,commons-collections- 3.1.jar,slf4j-api-1.7.12.jar,slf4j-log4j12-1.7.12.jar这几个jar包就可以正常运行了
当然,删掉新的jar包去用旧的jar包这种方式我个人认为是不太恰当的,因此有了方案2
方案2:
原来的jar包不变
(1)添加:hibernate-proxool-5.0.2.Final.jar,路径:hibernate-release-5.0.2.Final/lib/optional/proxool/hibernate-proxool-5.0.2.Final.jar
最终,WEB-INF/lib下有这些jar包:
(2)修改hibernate.cfg.xml:
<property name="hibernate.connection.provider_class">org.hibernate.proxool.internal.ProxoolConnectionProvider</property> 注意看,这里的路径变了。我这里的完整的hibernate.cfg.xml是这样的:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.proxool.pool_alias">ProxoolPool</property> <property name="hibernate.proxool.xml">proxool.xml</property> <property name="hibernate.connection.provider_class">org.hibernate.proxool.internal.ProxoolConnectionProvider</property> <property name="hibernate.proxool.existing_pool">true</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <mapping resource="com/hibtest1/entity/Users.hbm.xml" /> </session-factory> </hibernate-configuration>
可以看出,已经很正常了,问题到此解决