Tomcat 6,JPA和数据源

oni*_*unn 3 java tomcat jpa

我正在尝试在我的jsf应用程序中使用数据源.我在web-apps context.xml中定义了数据源

web应用/ META-INF/context.xml的

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Sale">
<Resource auth="Container" 
   driverClassName="com.mysql.jdbc.Driver" 
   maxActive="20" 
   maxIdle="10" 
   maxWait="-1" 
   name="Sale" 
   password="admin" 
   type="javax.sql.DataSource" 
   url="jdbc:mysql://localhost/sale" 
   username="admin"/>
</Context>
Run Code Online (Sandbox Code Playgroud)

web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
    30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/welcomeJSF.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>ruby</param-value>
</context-param>
</web-app>
Run Code Online (Sandbox Code Playgroud)

和我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="SalePU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<non-jta-data-source>Sale</non-jta-data-source>
<class>org.comp.sale.AnfrageAnhang</class>
<class>org.comp.sale.Beschaffung</class>
<class>org.comp.sale.Konto</class>
<class>org.comp.sale.Anfrage</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)

但是Tomcat似乎没有创建数据源,我只得到这个例外

Exception [TOPLINK-7060] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: Cannot acquire data source [Sale].
Internal Exception: javax.naming.NameNotFoundException: Name Sale is not bound in this Context
Run Code Online (Sandbox Code Playgroud)

MySQL驱动程序所需的jar包含在WEB-INF/lib目录中.

我做错了什么?

Pas*_*ent 6

<non-jta-data-source>Sale</non-jta-data-source>看起来不正确,你应该使用这种格式<non-jta-data-source>java:comp/env/ds/OracleDS</non-jta-data-source>(至少这是我对文档的理解).

而且我实际上并不确信您的JDBC数据源JDNI资源是否已正确创建(因为您将jdbc驱动程序jar放入其中WEB-INF/lib).从Tomcat文档:

使用JDBC数据源JNDI资源工厂要求您为Tomcat内部类和Web应用程序提供适当的JDBC驱动程序.通过将驱动程序的JAR文件安装到$CATALINA_HOME/common/lib 目录中可以很容易地实现这一点,这使得驱动程序既可用于资源工厂,也可用于应用程序.

您应该首先测试它(通过编写快速代码进行查找以获得连接).

也严格按照所描述的步骤的EclipseLink /实施例/ JPA /的Tomcat Web教程(和对准的内容web.xml,context.xmlpersistence.xml).