• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Configure tomcat resource factory

 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot seem to get this tomcat configuration to work. Here is what I do.
In my connection bean below is my code:


import java.sql.*;
import java.io.*;
import java.text.*;
import java.util.*;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class connectionBean{

public connectionBean(){

}

public static Connection getConnection(){
Connection cnt=null;
try{
InitialContext initialContext=new InitialContext();
Context context=(Context)initialContext.lookup("java:comp/env");
DataSource ds=(DataSource) context.lookup("jdbc/PMDB");
cnt=ds.getConnection();
}catch(Exception exp){}


return cnt;
}
}



In web.xml i put in the <resource-ref> at the top as below:

<?xml version="1.0"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- General description of the Application -->
<resource-ref>
<res-ref-name>jdbc/PMDB</res-ref-name>
<res-type>jdbc/PMDB</res-type>
<res-auth>jdbc/PMDB</res-auth>
</resource-ref>

<display-name>scrmypm</display-name>
<description>
This is the development application
</description>
<servlet>
<display-name>ProServlet</display-name>
<servlet-name>ProServlet</servlet-name>
<servlet-class>Servlet.ProServlet</servlet-class>
</servlet>
...


In the conf/server.xml I put in the Resource Param below towards the end.

<Context className="org.apache.catalina.core.StandardContext" cachingAllowed="true" charsetMapperClass="org.apache.catalina.util.CharsetMapper" cookies="true" crossContext="false" debug="0" docBase="C:\jwsdp-1_0_01\webapps\ROOT" mapperClass="org.apache.catalina.core.StandardContextMapper" path="" privileged="false" reloadable="false" useNaming="true" wrapperClass="org.apache.catalina.core.StandardWrapper">
</Context>

<DefaultContext>
<Resource name="jdbc/PMDB" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/PMDB">
<parameter>
<name>username</name>
<value>user</value>
</parameter>
<parameter>
<name>password</name>
<value>password</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.9.201.28/PMDB</value>
</parameter>
</ResourceParams>
</DefaultContext>
</Host>
<Logger className="org.apache.catalina.logger.FileLogger" debug="0" directory="logs" prefix="jwsdp_log." suffix=".txt" timestamp="true" verbosity="1"/>
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" debug="0" resourceName="UserDatabase" validate="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" debug="0" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="access_log." resolveHosts="false" suffix=".txt"/>
</Engine>
</Service>
</Server>


Anyone with ideas?
 
Vilpesh Mistry
Ranch Hand
Posts: 62
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
please change in web.xml entries from

<resource-ref>
<res-ref-name>jdbc/PMDB</res-ref-name>
<res-type>jdbc/PMDB</res-type>
<res-auth>jdbc/PMDB</res-auth>
</resource-ref>

to
<resource-ref>
<res-ref-name>jdbc/PMDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


thanks
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error in the log file printed

ErrorDataSource Factory bcpDataSourceFactory: username is required

But already specify in server.xml. Do i need to specify anywhere else. Thanks
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When restarting tomcat i get this unusual error

[ERROR] Digester - -Parse Error at line 2 column 219: Document root element "web-app", must match DOCTYPE root "null". <org.xml.sax.SAXParseException: Document root element "web-app", must match DOCTYPE root "null".>
org.xml.sax.SAXParseException: Document is invalid: no grammar found.


Then when i access the database the error is:

[ERROR] Digester - -Parse Error at line 2 column 219: Document is invalid: no grammar found. <org.xml.sax.SAXParseException: Document is invalid: no grammar found.>
ErrorDataSource Factory bcpDataSourceFactory: username is required

Hope some one know about xml configuration.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something wrong with the format of that web.xml file in the vicinity of line 2 - as a consequence, no part of that web application will work. It may be something very simple, examine web.xml carefully and compare the starting lines with a web.xml that works.
Bill
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the web.xml now into the below.

<?xml version="1.0"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<resource-ref>
<res-ref-name>jdbc/PMDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<servlet>
<servlet-name>MercProServlet</servlet-name>
<servlet-class>PMServlet.MercProServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>MercProUpdServlet</servlet-name>
<servlet-class>PMServlet.MercProUpdServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>AcctProsServlet</servlet-name>
<servlet-class>PMServlet.AcctProsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MercProServlet</servlet-name>
<url-pattern>/MercProServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MercProUpdServlet</servlet-name>
<url-pattern>/MercProUpdServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AcctProsServlet</servlet-name>
<url-pattern>/AcctProsServlet</url-pattern>
</servlet-mapping>
</web-app>

And then i got this error below in the log file:

[ERROR] Digester - -Parse Error at line 141 column 11: The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)". <org.xml.sax.SAXParseException: The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)".>

I have try putting in on top and below and still ended with error. I am confused as to how to put the data source declaration in the web.xml
It seems cannot put both data source declaration and servlet mapping declaration together. Thanks for any help.
 
X. Li
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The element order in web.xml is important: You shouldn't put <resource-ref> element on the top, it must follow element <servlet-mapping> like this:

<servlet>
...
</servlet>
<servlet-mapping>
...
</servlet-mapping>
<resource-ref>
...
</resource-ref>
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also tried the solution by putting at below but still the same error.
 
X. Li
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you don't have to put these elements in web.xml. You only need to add following in TOMCAT_HOME/conf/context.xml (this is for Tomcat 5.X, for 4.x, you may have to put it within <context> in server.xml):

<Context path="/MyTest" reloadable="true" docBase="C:\Eclipse\workspace\MyTest\myWeb" workDir="C:\Eclipse\workspace\MyTest\myWeb\work" >

<Resource name="jdbc/mydb2" auth="Container"
type="javax.sql.DataSource"
driverClassName="COM.ibm.db2.jdbc.app.DB2Driver" url="jdbc b2:mydb"
username="youruser" password="yourpass"
maxActive="8" maxIdle="4"/>

</Context>
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try out the solution and put the below in server.xml

<Context path="/scrmypm" reloadable="true" docBase="D:\jwstutorial13\examples\web\scrmypm" workDir="D:\jwstutorial13\examples\web\scrmypm\build" >

<Resource name="jdbc/PMDB" auth="Container"
type="javax.sql.DataSource"
driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://xxx.xx.xxx.xxx/PMDB"
username="user" password="password"
maxActive="8" maxIdle="4"/>
</Context>

When I run the application it came out FAIL- application already exists at path /scrmypm
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic