gpt4 book ai didi

java - Spring/Hibernate配置错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:13:11 25 4
gpt4 key购买 nike

我是 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 文件夹中的调度程序 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 中找到数据源引起的,如果我错了,请纠正我。

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

最佳答案

JNDI 资源通常很难弄清楚。

Stacktrace 指出数据源的定义方式存在问题。根据documentation :

Expect to find the JNDI name of a JDBC resource in java:comp/env/jdbc

尝试将以下内容放入您的配置中:

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

虽然根据这个问题:Spring 3 JNDI look up in glassfish3这是没有必要的

关于java - Spring/Hibernate配置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20338233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com