Based on Tomcat41 docs, jndi-datasource-examples-howto.html, have tried to setup a
database connection pool as follows, but get result:
Foo Not Connected
Bar -1
generated from following jsp code, as per howto given above:
<%
foo.DBTest tst = new foo.DBTest();
tst.init();
%>
<%= tst.getFoo() %>
<%= tst.getBar() %>
Not sure what i am doing wrong. If anyone has any ideas, or suggestioins as to how
to go about trouble-shooting, please let me know (see below for config info used).
-thanks, paul.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
software system:
win2k
java jdk 1.4
jakarta-tomcat-4.1.27-LE-jdk14.exe
apache_2.0.47-win32-x86-no_ssl.msi
jk2 connector
%CATALINA_HOME%\common\lib:
commons-collections-2.1.jar
commons-dbcp-1.0.jar
commons-pool-1.0.1.jar
plus: oracle jdbc driver 9.x with .jar extension
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
%CATALINA_HOME%\conf\server.xml:
<!-- Add btween </Context> of examples context & </Host> closing localhost definition
-->
<Context path="/epl" docBase="epl" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt" timestamp="true"/>
<!-- Define a Datasource -->
<Resource name="jdbc/eplResrc" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/eplResrc">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Set to 0 for no limit -->
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>scott</value>
</parameter>
<parameter>
<name>password</name>
<value>tiger</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@pl-wkstn:1521:www</value>
</parameter>
<!-- To configure a DBCP DataSource, abandoned dB connections are removed and
recycled -->
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<!-- number of seconds a dB connection has been idle before it is considered
abandoned -->
<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
</ResourceParams>
</Context>
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
%WEB-INF\web.xml
<?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>
<description>db Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/eplResrc</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
%WEB-INF%\classes\foo\DBTest.java
package foo;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class DBTest {
String foo = "Not Connected";
int bar = -1;
public void init() {
try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
DataSource ds =
(DataSource)ctx.lookup(
"java:comp/env/jdbc/eplResrc");
if (ds != null) {
Connection conn = ds.getConnection();
if(conn != null) {
foo = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst =
stmt.executeQuery("select sysdate from dual");
if(rst.next()) {
foo=rst.getString(2);
bar=rst.getInt(3);
}
conn.close();
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
public String getFoo() { return foo; }
public int getBar() { return bar;}
}