我正在尝试让一个数据源在我的jsf应用程序中工作。我在我的web应用程序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 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>还有我的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>但是似乎没有数据源是由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 ContextMySQL驱动程序所需的jars包含在WEB-INF/lib目录中。
我哪里做错了?
发布于 2010-03-18 07:15:11
您的<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 driver jar放在WEB-INF/lib中)。来自Tomcat文档:
JNDI Resource Factory的
使用要求您为Tomcat内部类和您的web应用程序提供适当的JDBC驱动程序。最容易做到这一点的方法是将驱动程序的JAR文件安装到
$CATALINA_HOME/common/lib目录中,这将使驱动程序对资源工厂和您的应用程序都可用。
您可能应该首先对此进行测试(通过编写一段快速代码,执行查找以获取连接)。
还要严格遵循EclipseLink/Examples/JPA/Tomcat Web Tutorial中描述的步骤(并对齐web.xml、context.xml和persistence.xml的内容)。
发布于 2012-01-10 22:15:15
正如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> 正确的叫法是:
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:/comp/env/jdbc/sadep");因此,对于使用tomcat的JPA:
<non-jta-data-source>java:/comp/env/jdbc/sadep</non-jta-data-source>发布于 2010-03-17 17:16:33
我认为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>https://stackoverflow.com/questions/2460702
复制相似问题