Setting up Tomcat 5.5, Oracle, JSTL, with connection pool

[nobr]Hi I'm trying to setup the above, going thru the Apache Tomcat 5.5 Servlet/JSP Container HOW-TO at:

http://tomcat.apache.org/tomcat-5.5-doc/printer/jndi-datasource-examples-howto.html

I have added this to the server.xml file:

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"

maxActive="100" maxIdle="30" maxWait="10000"

username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>

I then added this to my applications web.xml file:

<web-app 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"

version="2.4">

<description>MySQL Test App</description>

<resource-ref>

<description>DB Connection</description>

<res-ref-name>jdbc/TestDB</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

</web-app>

Using this simple test page:

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<sql:query var="rs" dataSource="jdbc/TestDB">

select id, foo, bar from testdata

</sql:query>

<html>

<head>

<title>DB Test</title>

</head>

<body>

<h2>Results</h2>

<c:forEach var="row" items="${rs.rows}">

Foo ${row.foo}<br/>

Bar ${row.bar}<br/>

</c:forEach>

</body>

</html>

I should be able to query my oracle database, I put in my own database values , of course.

My point of confusion is where I place the following code:

Context initContext =new InitialContext();

Context envContext = (Context)initContext.lookup("java:/comp/env");

DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");

Connection conn = ds.getConnection();

//etc.

It would seem this code should go into a servlet, but the way it's stated, it seems like it goes in one of the configuration files.

I am trying to follow the MVC design pattern using JSTL so I want to keep all code out of the JSP.

Thank you for your help!

Alan[/nobr]

[3384 byte] By [shadow_codera] at [2007-10-2 16:45:55]
# 1

The tutorial your following is a good one, you will get it working in the end....

I used mySQL, but will do my best.

Did you start the server.xml config with

<Context path="/DBTest" docBase="DBTest"

debug="5" reloadable="true" crossContext="true">

as was the case in the mySql tutorial?

If you look, the Oracle stuff on server.xml just covers the <resource> section, it still needs to be nested in a context.

Have you copied the ORACLE driver you need into the tomcat/common/lib folder?

If not this is a .jar file you will have to find and download, then just save it in above mentioned folder.

The code example is an ALTERNATIVE METHOD to the code beginning

<sql:query var="rs" dataSource="jdbc/TestDB">

The last code example is showing how to access JNDI using only Java.

Hope that makes sense!

angrycata at 2007-7-13 17:56:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I never had much luck putting this config into server.xml

But there is an alternative:

Put the config into a seperate "context.xml" file under [TOMCAT]/conf\Catalina\localhost, as documented here:

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

eg in [TOMCAT]/conf/Catalina/localhost/testJDBC.xml

<Context path="/testJDBC" docBase="C:\Java\Tomcat 5.0\webapps\testing">

<Resource name="jdbc/TestDB" auth="Container"

type="javax.sql.DataSource">

</Resource>

<ResourceParams name="jdbc/TestDB" >

<parameter>

<name>factory</name>

<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>

</parameter>

<parameter>

<name>driverClassName</name>

<value>com.mysql.jdbc.Driver</value>

</parameter>

<parameter>

<name>url</name>

<value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>

</parameter>

<parameter>

<name>username</name>

<value>javauser</value>

</parameter>

<parameter>

<name>password</name>

<value>javadude</value>

</parameter>

<parameter>

<name>maxActive</name>

<value>100</value>

</parameter>

<parameter>

<name>maxIdle</name>

<value>30</value>

</parameter>

<parameter>

<name>maxWait</name>

<value>-10000</value>

</parameter>

</ResourceParams>

</Context>

Hope this helps,

evnafets

evnafetsa at 2007-7-13 17:56:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Its even better to put the params as attributes rather use the Resource params tag.

So do just as evnafets said, define your own context xml (the name of the xml file should be <your_contextname>.xml) and put this in <TOMCAT_INSTALL>/conf/CATALINA/localhost directory.

<Context path="/testJDBC" docBase="C:\Java\Tomcat 5.0\webapps\testing">

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"

maxActive="100" maxIdle="30" maxWait="10000"

username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>

</Context>

The docBase attribute is the path to your web application on your desktop. You needn't put it in the webapps folder :). Plcae the app anywhere on the desktop and just give the path in the docBase attribute.

ram.

Madathil_Prasada at 2007-7-13 17:56:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Thank you for your replies,

I was able to work on the solutions you have provided, I cannot get it working when I change the server.xml file, this method isn't recommended for tomcat 5 anyways according to the reference evnafets provided.

I then tried evnafets method but am getting an error "unable to get connection, DataSource invalid, no driver found" I have the driver in C:\Tomcat5516\common\lib and have also tried it in <myapp>\WEB-INF\lib

I do have the most recent oracle driver installed. Another question, do you still need a web.xml file in your applications WEB-INF folder using the method evnafets describes?

Thanks again for your help!

shadow_codera at 2007-7-13 17:56:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Ok, back again! I found a typo, I also realized you do need a web.xml in each application. My error is now thus:Cannot create JDBC driver of class ' ' for connect URL 'null'.I'm going to keep working on this, seems like it's finding the driver though.
shadow_codera at 2007-7-13 17:56:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

I have it figured out.

What I have to do was put the put just the context.xml file where you said to, with oracle it didn't like the end of the URL that said "autoReconnect="true".

I also found out that with this solution a separate web.xml file could be used but isn't necessary, it would be redundant.

I beileve all three solutions may have worked, i'll go back and explore this at a later time.

I'm going to award points to everyone who was nice enough to offer suggestions, thanks again!

shadow_codera at 2007-7-13 17:56:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Hi,Since I do have this setup and working now, I still am using a query in the jsp. Should this go into the applications web.xml file or should it go into a servlet?Thanks!
shadow_codera at 2007-7-13 17:56:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...