tags:

views:

732

answers:

2

Hi there,

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"&gt;
<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"&gt;
<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?

A: 

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>
JoseK
Thought the same, tested it too, but no affect. Anyway the Tomcat doc seems to be clear about thathttp://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html#context.xml%20configuration
asrijaal
How about this note "Note: docBase is very important - it must match your WAR file or you will get a (Name ds is not bound in this Context) exception " on this link http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial#Non-JTA_Datasource
JoseK
+2  A: 

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).

Pascal Thivent
`<non-jta-data-source>java:comp/env/Sale</non-jta-data-source>` given the `name="Sale"` in this question.
Arjan