在spring xml中配置hibernate数据源

时间:2013-11-23 09:52:05

标签: spring hibernate datasource

运行我的Web应用程序时出现以下错误。我在配置hibernate和amp;时遇到了问题。春天用右罐子。在我工作了几个小时之后我跨过了这个障碍,我在 hibernate数据源中遇到了问题。会话工厂和dao bean正在春天正确初始化 但是当我在dao中访问方法时我得到错误...

    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)

以下是我的应用程序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>

我使用 spring3&amp; hibernate4 即可。看来我的数据源中存在配置错误。但我无法找到它。

从下面的服务器日志中,我看到dao&amp;会话工厂bean正确实例化。

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'

请帮助..

5 个答案:

答案 0 :(得分:1)

根据最新版本,不支持方法getConnection(user, password),但hibernate不应该调用该方法。

您可以使用MySQL连接器包中提供的弹簧org.springframework.jdbc.datasource.DriverManagerDataSourcecom.mysql.jdbc.jdbc2.optional.MysqlDataSource。 (http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html

答案 1 :(得分:1)

尝试从hibernate.cfg.xml中删除

<property name="hibernate.connection.password">your-password</property>
<property name="hibernate.connection.username">your-username</property>

答案 2 :(得分:1)

的问题
org.springframework.jdbc.datasource.DriverManagerDataSource

它不提供汇集!我将转移到Tomcat JDBC连接池。到目前为止,恕我直言是最有效率的。

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">

它需要依赖

org.apache.tomcat tomcat-jdbc

检查文件: https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html

答案 3 :(得分:0)

尝试在hibernate属性中添加以下代码

      

         <prop key=" hibernate.dialect "> org.hibernate.dialect.MySQLDialect </prop>
         <prop key="hibernate.show_sql">true</prop>
         <prop key="hibernate.connection.username">root</prop>
         <prop key="hibernate.connection.password">root</prop>

  </props>

答案 4 :(得分:-1)

尝试使用com.mysql.jdbc.jdbc2.optional.MysqlDataSource。