Sun bundles Tomcat 4 with Solaris 9 and i'm having problems getting the
datasource to work
i am trying to connection to a Sybase ASE database and so i went to
Sybase's website for help and they have jConnect 5.5 (a .jar file that i
guess contains their driver)
i copied the jConnect5.5 jconn2.jar file to /usr/apache/tomcat/common/lib
i pasted the new context (for connecting to the database that was provided
by Sybase) into the server.xml file (see this context below) in
/var/apache/tomcat/conf
the directory tree for the webapps looks like so:
/var/apache/tomcat/webapps/sybase/jsp/sybase.jsp
/var/apache/tomcat/webapps/sybase/WEB-INF/web.xml
/var/apache/tomcat/webapps/sybase/WEB-INF/jsp/
i then stopped and then re-started apache
my sybase.jsp file looks like this:
------------------------------
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="java.util.*" %>
<html>
<head> <title> Testing Sybase DataSource </title> </head>
<body bgcolor="white">
<font size=4>
<h3> Start of Test </h3>
<%
String s = "java:comp/env";
String t = "jdbc/a_sybase_datasource";
InitialContext initCtx = null;
try
{
initCtx = new InitialContext();
out.println("<p>initCtx = new InitialContext() : successful");
out.println("<BR>");
out.println( initCtx );
}
catch (Exception e)
{
out.println("<p>initCtx = new InitialContext() : failed");
out.println("<BR>");
out.println ( e );
}
Context envCtx = null;
try
{
envCtx = (Context) initCtx.lookup( s );
out.println("<BR>envCtx = initCtx.lookup(" + s + ") : successful");
out.println("<BR>");
out.println( envCtx );
}
catch (Exception e)
{
out.println("<BR>envCtx = initCtx.lookup(" + s + ") : failed");
out.println ( e );
}
DataSource ds = null;
ds = null;
try
{
out.println("<p>Trying ds=(DataSource)envCtx.lookup(" + t + ")");
ds = (DataSource) envCtx.lookup( t );
out.println("<BR>DataSource lookup apparently successful");
out.println("<BR>" + ds );
}
catch (Exception e)
{
out.println("<BR>Datasource lookup failed <BR>");
out.print( e );
}
if (ds == (DataSource) null)
{
out.println("<BR>DataSource null\n");
}
try {
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select db_name()");
while (rs.next())
{
out.println
( "<BR>Inside fetch loop:" );
out.println
( "<BR>Value Returned from DataSource: " +
rs.getString(1) );
} // end_while
rs.close();
stmt.close();
conn.close();
} // end_try
catch (SQLException sqe) {
out.println("<BR>Inside catch(SQLException sqe)");
out.println( sqe.getMessage() );
} // end catch SQLException
catch (Exception e) {
out.println("<BR>Inside catch(Exception e)<BR>");
out.println(e.getMessage());
out.print( e );
} // end catch Exception
%>
<p>
<h3> End of Test </h3>
</font>
</body>
</html>
------------------------------
my web.xml file looks like this:
------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
<welcome-file-list>
<welcome-file> /jsp/sybase.jsp </welcome-file>
</welcome-file-list>
<!-- for using dscp -->
<resource-ref>
<res-ref-name> jdbc/a_sybase_datasource </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
</web-app>
------------------------------
context in server.xml
------------------------------
<!-- Sybase Example Context - dscp example -->
<Context path="/sybase"
docBase="sybase"
debug="5"
reloadable="true"
crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_Sybase.log."
suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/a_sybase_datasource"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/a_sybase_datasource">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.sybase.jdbc2.jdbc.SybDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:sybase:Tds:10.34.34.34:4300/mydb?JCONNECT_VERSION=6</value>
</parameter>
<parameter>
<name>username</name>
<value>myuser</value>
</parameter>
<parameter>
<name>password</name>
<value>mypasswd</value>
</parameter>
</ResourceParams>
</Context>
<!-- END Sybase Example Context - dscp example -->
-------------------------------------
when i call /var/apache/tomcat/webapps/sybase/jsp/sybase.jsp (it's a
connection test file that Sybase provides) through
http://hostname:port/sybase/jsp/sybase.jsp
i get this error:
-------------------------------------
Start of Test
initCtx = new InitialContext() : successful
[EMAIL PROTECTED]
envCtx = initCtx.lookup(java:comp/env) : successful
[EMAIL PROTECTED]
Trying ds=(DataSource)envCtx.lookup(jdbc/a_sybase_datasource)
Datasource lookup failed
javax.naming.NamingException: Cannot create resource instance
DataSource null
Inside catch(Exception e)
null java.lang.NullPointerException
End of Test
-------------------------------------
appears that in sybase.jsp, the line:
ds = (DataSource) envCtx.lookup( t );
fails ... any ideas, anyone?
Thanks,
Alan Czajkowski
-------------------------------------
Database Administrator
BMO Financial Group
Decision Support Services
3300 Bloor Street West
14th Floor, West Tower
Toronto, Ontario, M8X 2X2
Tel: 416.232.8736
-------------------------------------