【问题标题】:Hibernate cannot find suitable driver for Derby in Spring MVC webappHibernate 在 Spring MVC webapp 中找不到适合 Derby 的驱动程序
【发布时间】:2015-07-01 20:37:54
【问题描述】:

我想知道是否有人可以向我解释为什么 Spring 和 Hibernate webapp 在两种环境中运行良好,但在另一种环境中却失败了?我将NetBeans 8.0.XTomcat 8.0.3.0Apache Derby 10.X 一起使用。

我的申请dispatcherservlet如下:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc" 
   xmlns:tx="http://www.springframework.org/schema/tx"       
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
                       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
                       http://www.springframework.org/schema/context 
                       http://www.springframework.org/schema/context/spring-context-4.0.xsd 
                       http://www.springframework.org/schema/mvc                            
                       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd                           
                       http://www.springframework.org/schema/tx    
                       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- Uses annotations in classes for JavaBeans. XML is an alternative. -->
<mvc:annotation-driven />   

<!-- Base package. -->
<context:component-scan base-package="library" />    

<!-- Model. -->
<bean id="person" class="library.model.Person" />
<bean id="book" class="library.model.Book" />        

<!-- Spring Controllers. -->
<bean id="adminController" class="library.controller.admin.AdminController" />    
<bean id="personController" class="library.controller.PersonController" />      
<bean id="bookController" class="library.controller.BookController" /> 
<bean id="exceptionController" class="library.controller.ExceptionController" />     

<!-- Spring Interceptors. -->
<mvc:interceptors>
    <bean id="clientInterceptor" class="library.interceptor.ClientInterceptor" />
</mvc:interceptors>

<!-- Spring Services. -->
<bean id="personServiceImpl" class="library.service.PersonServiceImpl" />    
<bean id="bookServiceImpl" class="library.service.BookServiceImpl" />      

<!-- Spring Repositories. -->
<bean id="personDAOImpl" class="library.dao.PersonDAOImpl" />    
<bean id="bookDAOImpl" class="library.dao.BookDAOImpl" />       

<!-- Spring Validators. -->
<bean id="personValidator" class="library.validator.PersonValidator" />    
<bean id="bookValidator" class="library.validator.BookValidator" />     

<!-- Spring ViewResolver. -->               
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
         <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>   

<!-- Spring MesssageSource. -->         
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename"> 
        <value>/WEB-INF/classes/messages</value>    
    </property>    
</bean>

<!-- Spring Properties file for Library. -->      
<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location">  
         <value>classpath:library.properties</value>             
    </property>
</bean>      

<!-- Hibernate DataSource. -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
    <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />        
    <!--property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" /-->
    <property name="url" value="jdbc:derby://localhost:1527/Library" />
    <property name="username" value="username" />
    <property name="password" value="password" />            
</bean>   

<!-- Hibernate Interceptors. -->
<bean id="serverInterceptor" class="library.interceptor.ServerInterceptor" />

<!-- Hibernate SessionFactory. -->    
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">    
    <property name="dataSource" ref="dataSource"></property>                  
    <property name="hibernateProperties">
        <props>    
           <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>                     
           <!--prop key="hibernate.dialect">org.hibernate.dialect.DerbyTenSixDialect</prop-->
           <prop key="hibernate.show_sql">false</prop>               

           <!-- What to do with the database schema. -->
           <prop key="hbm2ddl.auto">validate</prop>    
           <!-- validate:    validate the schema, makes no changes to the database.
                update:      update the schema.
                create:      creates the schema, destroying previous data.
                create-drop: drop the schema at the end of the session. -->                 
        </props>            
    </property>                                                                                                                                
    <property name="entityInterceptor">
        <ref bean="serverInterceptor" />            
    </property>                                                                                                                                             
    <property name="packagesToScan">
        <list>
            <value>library.model</value>                
        </list>
    </property>          
</bean>

<!-- Hibernate TransactionManagment. -->
<tx:annotation-driven />                
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
</bean>  

当应用程序在一个环境中构建和失败时给出的错误是:

WARN|01 07 2015|16 26 31|http-nio-8080-exec-73|org.hibernate.engine.jdbc.internal.JdbcServicesImpl| - HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:derby://localhost:1527/Library
INFO|01 07 2015|16 26 31|http-nio-8080-exec-73|org.hibernate.dialect.Dialect| - HHH000400: Using dialect: org.hibernate.dialect.DerbyTenSixDialect
INFO|01 07 2015|16 26 34|http-nio-8080-exec-73|org.hibernate.engine.jdbc.internal.LobCreatorBuilder| - HHH000422: Disabling contextual LOB creation as connection was null
ERROR|01 07 2015|16 26 41|http-nio-8080-exec-73|org.springframework.web.context.ContextLoader| - Context initialization failed

这会导致依赖关系的连接失败。

这些消息是什么意思?

应用程序正在使用 Spring 4.0.2hibernate-core-4.3.10.jar。三个环境之间的所有依赖项都是相同的,它们的列表如下:

30 Jun 2015  20 26           445,288 antlr-2.7.7.jar
30 Jun 2015  20 26             4,467 aopalliance-1.0.jar
30 Jun 2015  20 26           160,519 commons-dbcp-1.4.jar
30 Jun 2015  20 26            62,050 commons-logging-1.1.3.jar
30 Jun 2015  20 26         2,834,700 derby.jar
30 Jun 2015  20 26           582,639 derbyclient.jar
30 Jun 2015  20 26           313,898 dom4j-1.6.1.jar
30 Jun 2015  20 26            75,311 hibernate-commons-annotations-4.0.4.Final.jar
30 Jun 2015  20 26         5,280,098 hibernate-core-4.3.10.Final.jar
30 Jun 2015  20 27           113,371 hibernate-jpa-2.1-api-1.0.0.Final.jar
30 Jun 2015  20 26            38,605 jackson-annotations-2.4.0.jar
30 Jun 2015  20 26           225,306 jackson-core-2.4.1.jar
30 Jun 2015  20 27           228,552 jackson-core-asl-1.9.7.jar
30 Jun 2015  20 26         1,074,275 jackson-databind-2.4.1.jar
30 Jun 2015  20 26           786,084 jackson-mapper-lgpl-1.9.13.jar
30 Jun 2015  20 26            76,551 jandex-1.1.0.Final.jar
30 Jun 2015  20 26           714,194 javassist-3.18.1-GA.jar
30 Jun 2015  20 26           162,126 javax.persistence-2.1.0.jar
30 Jun 2015  20 26            57,183 jboss-logging-3.1.3.GA.jar
30 Jun 2015  20 26            11,558 jboss-logging-annotations-1.2.0.Beta1.jar
30 Jun 2015  20 27            27,717 jboss-transaction-api_1.2_spec-1.0.0.Final.jar
30 Jun 2015  20 26            20,682 jstl-1.1.2.jar
30 Jun 2015  20 26            15,071 jta-1.1.jar
30 Jun 2015  20 26           367,444 log4j-1.2.14.jar
30 Jun 2015  20 27            52,150 persistence-api-1.0.jar
30 Jun 2015  20 27            36,364 spring-annotation-base-1.0.2.jar
30 Jun 2015  20 26           352,730 spring-aop-4.0.2.RELEASE.jar
30 Jun 2015  20 26           669,044 spring-beans-4.0.2.RELEASE.jar
30 Jun 2015  20 26           974,272 spring-context-4.0.2.RELEASE.jar
30 Jun 2015  20 26           960,994 spring-core-4.0.2.RELEASE.jar
30 Jun 2015  20 26           204,780 spring-expression-4.0.2.RELEASE.jar
30 Jun 2015  20 26           419,614 spring-jdbc-4.0.2.RELEASE.jar
30 Jun 2015  20 26           366,844 spring-orm-4.0.2.RELEASE.jar
30 Jun 2015  20 26           248,204 spring-tx-4.0.2.RELEASE.jar
30 Jun 2015  20 26           665,015 spring-web-4.0.2.RELEASE.jar
30 Jun 2015  20 26           660,329 spring-webmvc-4.0.2.RELEASE.jar
30 Jun 2015  20 26           393,259 standard-1.1.2.jar

【问题讨论】:

  • 你说编译失败。但错误消息来自运行时。你什么时候准确地得到错误?您还可以分享您对这两个环境的依赖关系吗?您确定您的 derby 驱动程序的 jar 在您的类路径中吗?
  • 在项目被编译为运行它的一部分之后,就会出现问题。问题中列出了依赖项。

标签: java spring hibernate jdbc


【解决方案1】:

您的库中似乎不存在 derbyclient.jar,因此请将此 jar 添加到您的库中。从here下载

【讨论】:

  • derbyclient.jar 存在于依赖项中。
  • 那么你应该检查它的兼容版本。
  • 当前使用的derby.jarderbyclient.jar 版本已经使用JDK 1.71.8 的Derby 版本进行了测试。他们看起来很好。
【解决方案2】:

我认为这与您的连接字符串有关。可以这样试试吗?

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
      property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
    <property name="url" value="jdbc:derby:~/Library" />
    <property name="username" value="username" />
    <property name="password" value="password" />            
</bean>

这将在 $USER_HOME 上创建一个名为 Library 的数据库。

更新

默认 derby 不支持像 jdbc:derby://localhost/Libarary 这样的连接字符串。为了通过网络连接 derby 服务器,您将 derbynet.jar 添加到您的类路径中。你可以找到additional info here

然后您可以使用您的默认数据源定义。

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
    <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />        
    <property name="url" value="jdbc:derby://localhost:1527/Library" />
    <property name="username" value="username" />
    <property name="password" value="password" />            
</bean>  

【讨论】:

  • 我已经尝试过了,它会创建一个新的数据库,但是我必须修改回&lt;property name="url" value="jdbc:derby:~/Library" /&gt;。然而,这无法连接,所以我必须恢复到&lt;property name="url" value="jdbc:derby://localhost:1527/Library" /&gt;
  • @MrMorgan 我认为 derby 不支持 jdbc:derby://localhost:1527/Library 类型的连接字符串。您尝试连接的图书馆数据库的位置是什么?
  • 如果我在没有嵌入式驱动程序的情况下使用这些设置,而使用derbynet.jarderbyrun,jarderbytools.jar,则一切都在测试环境中运行顺利。我明天必须现场测试。
  • 即使使用这些设置,项目在编译后也无法加载。这个直播平台有什么问题?
  • @MrMorgan 这不是编译错误。它是一个运行时错误。您确定您的生产环境中有 derby 服务器吗?
猜你喜欢
  • 1970-01-01
  • 2017-12-20
  • 2011-12-22
  • 2018-06-09
  • 2019-11-12
  • 2013-03-05
  • 1970-01-01
  • 2012-12-09
相关资源
最近更新 更多