问题描述
运行我的web应用程序时出现以下错误。我在配置hibernate&春天与右罐子。在经过了几个小时之后,我跨过了这个障碍之后,我在 hibernate datasource 中遇到了问题。会话工厂和dao bean在spring
中正确初始化,但是当我在dao中访问方法时出错...
INFO:在调用rateDAO.getCount()之前
INFO:内部getSessionFactory()
警告:StandardWrapperValve [调度程序]:Servlet.service()用于servlet调度程序抛出异常
java。 lang.UnsupportedOperationException:在org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl在org.apache.commons.dbcp.BasicDataSource.getConnection通过的BasicDataSource
不支持(BasicDataSource.java:1062)
。的getConnection(DatasourceConnectionProviderImpl.java:141)
在org.hibernate.internal.AbstractSessionImpl $ NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
在org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection( LogicalConnectionImpl.java:214)
在org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
在org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection( StatementPreparerImpl.java:56)
在org.hibernate.engine.jdbc.internal.StatementPreparerImpl $ 5.doPrepare(StatementPreparerImpl.java:161)
在org.hibernate.engine.jdbc.internal.StatementPreparerImpl $ StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182)
。在org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:159)
在org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1859)
。在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
在org.hibernate.loader.Loader。 doQuery(Loader.java:900)
在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
在org.hibernate.loader.Loader.doList(Loader.java:2526)
at org.hibernate.loader.Loader.doList(Loader.java:2512)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
at org.hibernate.loader.Loader.list(Loader.java:2337)
在org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124)
在org.hibernate.internal。 SessionImpl.list(SessionImpl.java:1662)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
at daoImpl.RateDAO.getRateById(RateDAO.java:149)
at controller.Controller1.welcome(Controller1.java:47)
以下是我的 application context.xml ..
< bean id =myDataSourceclass =org.apache.commons .dbcp.BasicDataSourcedestroy-method =close>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / test_amar/>
< property name =usernamevalue =root/>
< property name =passwordvalue =/>
< property name =initialSizevalue =2>< / property>
< property name =validationQueryvalue =SELECT 1/>
< / bean>
< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =myDataSource/>
< property name =packagesToScan>
< array>
<值>实体< /值>
< / array>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =dialect> org.hibernate.dialect.MySQLDialect< / prop>
< /道具>
< / property>
< / bean>
我使用 spring3& hibernate4 即可。看起来我的数据源中存在配置错误。但我无法找到它。
从下面的服务器日志中,我看到dao&会话工厂bean正确实例化。
INFO:从ServletContext资源加载XML bean定义[/WEB-INF/dispatcher-servlet.xml]
INFO :为组件扫描找到并支持JSR-250'javax.annotation.ManagedBean'
为组件扫描发现并支持JSR-330'javax.inject.Named'注释
INFO:JSR-330' javax.inject.Inject'注释找到并支持自动装配
INFO:在org.springframework.beans.factory.support.DefaultListableBeanFactory@1020cb7中预先实例化单例:定义bean [** controller1,rateDAO **,org。 springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,组织。 springframework.web.servlet.mvc .support.ControllerClassNameHandlerMapping#0,urlMapping中,视图解析器,indexController的,org.springframework.context.annotation.ConfigurationClassPostProcessor $ ImportAwareBeanPostProcessor#0];父:org.springframework.beans.factory.support.DefaultListableBeanFactory@fca094
INFO:映射的URL路径[/ controller1]到处理程序'controller1'
INFO:映射的URL路径[/ controller1 / *]到处理程序'controller1'
请协助..
根据最新版本,不支持方法 getConnection(user,password)
,但hibernate不应该调用该方法。
你可以使用spring提供的 org.springframework.jdbc.datasource.DriverManagerDataSource
或 com.mysql.jdbc.jdbc2.optional.MysqlDataSource
从你的MySQL连接器包中获取。 ()
I get below error while running my web application. I had problems in configuring hibernate & spring with right jars. After i crossed that hurdle after working on it for hours, i am getting problem in hibernate datasource. The session factory and dao beans are getting initialized properly by springbut when i access method in dao i get error...
INFO: Attestation_spring_hibernate was successfully deployed in 11,309 milliseconds.
INFO: before calling rateDAO.getCount()
INFO: inside getSessionFactory()
WARNING: StandardWrapperValve[dispatcher]: Servlet.service() for servlet dispatcher threw exception
java.lang.UnsupportedOperationException: Not supported by BasicDataSource
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1062)
at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:56)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:161)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:159)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1859)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
at org.hibernate.loader.Loader.doQuery(Loader.java:900)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
at org.hibernate.loader.Loader.doList(Loader.java:2526)
at org.hibernate.loader.Loader.doList(Loader.java:2512)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
at org.hibernate.loader.Loader.list(Loader.java:2337)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1662)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
at daoImpl.RateDAO.getRateById(RateDAO.java:149)
at controller.Controller1.welcome(Controller1.java:47)
Below is my application context.xml..
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test_amar"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="initialSize" value="2"></property>
<property name="maxActive" value="5"></property>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<!-- Hibernate Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
<array>
<value>entities</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
i use spring3 & hibernate4. It appears there is configuration error in my datasource. but i am not able to locate it.
From the server log below , i see that dao & session factory beans are instantiated properly.
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
INFO: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1020cb7: defining beans [**controller1,rateDAO**,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0,urlMapping,viewResolver,indexController,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@fca094
INFO: Mapped URL path [/controller1] onto handler 'controller1'
INFO: Mapped URL path [/controller1/*] onto handler 'controller1'
Kindly assist..
Accoording to the latest version, the method getConnection(user, password)
is not supported, but hibernate should not be calling that method.
You can use the spring provided org.springframework.jdbc.datasource.DriverManagerDataSource
or the com.mysql.jdbc.jdbc2.optional.MysqlDataSource
from your MySQL connector package. (http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html)
这篇关于在spring xml中配置hibernate数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!