Reputation: 4778
I'm trying to get a data source working in my jsf app. I defined the data source in my web-apps 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>
and my 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
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.
What I'm doing wrong?
Upvotes: 3
Views: 14732
Reputation: 985
As Josek has said you need to make a datasource reference on web.xml file:
<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:
<non-jta-data-source>java:/comp/env/jdbc/sadep</non-jta-data-source>
Upvotes: 1
Reputation: 570315
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).
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:
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/lib
directory, which makes the driver available both to the resource factory and to your application.
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.xml
and persistence.xml
).
Upvotes: 6
Reputation: 31371
Think the web.xml also needs a reference to the datasource
<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>
Upvotes: 0