Spring / hibernate配置错误

时间:2013-12-02 21:29:45

标签: java spring hibernate spring-mvc glassfish

我是Java Spring和Hibernate的新手,我正在尝试使用这些技术(Maven,Spring和Hibernate)构建应用程序。当我尝试运行它时,我收到来自glassfish服务器的错误,这是一个非常长的堆栈跟踪,但这是最后一部分:

Exception while loading the app : java.lang.IllegalStateException:
ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
org.apache.catalina.LifecycleException:
javax.naming.NameNotFoundException: CentricJavaDB not found

我正在尝试几个小时,但我没有发现我的错误。我很确定它在配置xml文件中,但我无法修复它。我希望你们中的一些人可以帮助我(:

配置文件:

WEB-INF文件夹中的Dispatcher servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/jee
     http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="controllers"/>
    <context:component-scan base-package="service"/>
    <bean name = "klantDao" class="service.JpaKlantDao"/>

    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/CentricJavaDB"/>

    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>domain</value>
            </list>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" /> 
                <property name="generateDdl" value="true" />
             </bean>
        </property>
    </bean>

    <bean id="transactionManager" 
          class=" org.springframework.orm.jpa.JpaTransactionManager ">
        <constructor-arg ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
</beans>

WEB-INF中的glassfish-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
 <Resource name="jdbc/CentricJavaDB" auth="Container"
    driverClassName="com.mysql.jdbc.Driver"
    username="root"
    password="root"
    type="javax.sql.DataSource"
    url="jdbc:mysql://localhost:3306/centricjavadb" />
</glassfish-web-app>

我还发现了这个文件“Other Sources \ Setup \ glassfish-resources.xml”。我没有看到与“glassfish-web.xml”的巨大差异,但它也包含数据库信息。我认为这也很重要:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
    <jdbc-connection-pool allow-non-component-callers="false" 
                          associate-with-thread="false" 
                          connection-creation-retry-attempts="0" 
                          connection-creation-retry-interval-in-seconds="10"
                          connection-leak-reclaim="false" 
                          connection-leak-timeout-in-seconds="0" 
                          connection-validation-method="auto-commit" 
                          datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" 
                          fail-all-connections="false" 
                          idle-timeout-in-seconds="300" 
                          is-connection-validation-required="false" 
                          is-isolation-level-guaranteed="true" 
                          lazy-connection-association="false" 
                          lazy-connection-enlistment="false" 
                          match-connections="false" 
                          max-connection-usage-count="0" 
                          max-pool-size="32" 
                          max-wait-time-in-millis="60000"
                          name="jdbc/CentricJavaDB" 
                          non-transactional-connections="false" 
                          pool-resize-quantity="2" 
                          res-type="javax.sql.DataSource" 
                          statement-timeout-in-seconds="-1" 
                          steady-pool-size="8" 
                          validate-atmost-once-period-in-seconds="0" 
                          jndi-name="jdbc/CentricJavaDB"
                          wrap-jdbc-objects="false">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="3306"/>
        <property name="databaseName" value="centricjavadb"/>
        <property name="User" value="root"/>
        <property name="Password" value="root"/>
        <property name="URL" value="jdbc:mysql://localhost:3306/centricjavadb"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </jdbc-connection-pool> 
</resources>

我认为错误是由于jndi-lookup无法在glassfish-web.xml中找到数据源引起的,但如果我错了就纠正我。

我认为这应该是足够的信息。如果您想要更多文件,堆栈跟踪或其他任何内容,请询问,我会发布它。

1 个答案:

答案 0 :(得分:1)

JNDI资源通常很难搞清楚。

Stacktrace指出了如何定义数据源的问题。根据{{​​3}}:

  

期望在java中找到JDBC资源的JNDI名称:comp / env / jdbc

尝试在配置中添加以下内容:

 <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/CentricJavaDB"/>

虽然根据这个问题:documentation这不是必要的