java Tomcat 6、JPA 和数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2460702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tomcat 6, JPA and Data sources
提问by onigunn
I'm trying to get a data source working in my jsf app. I defined the data source in my web-apps context.xml
我正在尝试让数据源在我的 jsf 应用程序中工作。我在我的 web-apps context.xml 中定义了数据源
webapp/META-INF/context.xml
webapp/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>
web.xml
网页.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>
and my persistence.xml
和我的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>
But no data source seems to be created by Tomcat, I only get this exception
但是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
The needed jars for the MySQL driver are included into the WEB-INF/lib dir.
MySQL 驱动程序所需的 jars 包含在 WEB-INF/lib 目录中。
What I'm doing wrong?
我做错了什么?
回答by Pascal Thivent
Your <non-jta-data-source>Sale</non-jta-data-source>does not look correct, you should use the format <non-jta-data-source>java:comp/env/ds/OracleDS</non-jta-data-source>(at least this is my understanding of the documentation).
您<non-jta-data-source>Sale</non-jta-data-source>看起来不正确,您应该使用该格式<non-jta-data-source>java:comp/env/ds/OracleDS</non-jta-data-source>(至少这是我对文档的理解)。
And I'm actually not convinced that your JDBC datasource JDNI resource is properly created (because you put the jdbc driver jar in WEB-INF/lib). From the Tomcat documentation:
而且我实际上不相信您的 JDBC 数据源 JDNI 资源是否正确创建(因为您将 jdbc 驱动程序 jar 放在 中WEB-INF/lib)。从 Tomcat 文档:
Use of the JDBC Data Sources JNDI Resource Factory requires that you make an appropriate JDBC driver available to both Tomcat internal classes and to your web application. This is most easily accomplished by installing the driver's JAR file(s) into the
$CATALINA_HOME/common/libdirectory, which makes the driver available both to the resource factory and to your application.
JDBC 数据源的使用 JNDI 资源工厂要求您为 Tomcat 内部类和 Web 应用程序提供适当的 JDBC 驱动程序。这最容易通过将驱动程序的 JAR 文件安装到
$CATALINA_HOME/common/lib目录中来实现,这使得驱动程序可用于资源工厂和您的应用程序。
You should maybe test this first (by writing a quick piece of code doing a lookup to get a connection).
您可能应该先对此进行测试(通过编写一段快速的代码进行查找以获取连接)。
Also strictly follow the steps described in EclipseLink/Examples/JPA/Tomcat Web Tutorial(and align the content of web.xml, context.xmland persistence.xml).
还要严格按照EclipseLink/Examples/JPA/Tomcat Web Tutorial 中描述的步骤操作(并对齐web.xml、context.xml和的内容persistence.xml)。
回答by Gilberto
As Josek has said you need to make a datasource reference on web.xml file:
正如 Josek 所说,您需要对 web.xml 文件进行数据源引用:
<resource-ref>
<description>This is a PostgreSQL database connection</description>
<res-ref-name>jdbc/sadep</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
And the right way to call it is this:
正确的称呼方式是这样的:
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:/comp/env/jdbc/sadep");
So, for JPA using tomcat:
因此,对于使用 tomcat 的 JPA:
<non-jta-data-source>java:/comp/env/jdbc/sadep</non-jta-data-source>
回答by JoseK
Think the web.xml also needs a reference to the datasource
认为 web.xml 也需要对数据源的引用
<resource-ref>
<description>DB Connection</description>
<res-ref-name>Sale</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth> </resource-ref>

