Friday, January 8, 2010

Tomcat 5.X setup jndi

1. Copy jdbc driver to Tomcat 5.X\common\lib
2. Romove all commons-dbcp-x.x.x,commons-pool-x.x and jdbc driver from application/WEB-INF/lib

3. Add Context and jndi parameters in server.xml file as below:

Tomcat 5.0.xx:
(the entity way) was the only way that worked.

[server.xml]:
<Service...>
<Engine...>
<Host...>
...
<Context crossContext="true" docBase="/opt/tomcat5/webapps/oracleTest" path="/oracleTest" reloadable="true">
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/myoracle">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@15.1.1.40:1521:dbastra</value>
</parameter>
<parameter>
<name>username</name>
<value>uuuuuu</value>
</parameter>
<parameter>
<name>password</name>
<value>xxxxxxxx</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
<Resource name="jdbc/myoracle5" auth="Container"
type="javax.sql.DataSource" />
</Context>
</Host>
</Engine>
</Service>

Tomcat 5.5.xx:
(the attributes way) was the only way that worked.

[server.xml]:
<Service...>
<Engine...>
<Host...>
...
<Context path="/oracleTest" reloadable="true"
docBase="/opt/tomcat5/webapps/oracleTest">

<Logger className="org.apache.catalina.logger.SystemOutLogger"
verbosity="4" timestamp="true" />

<Resource name="jdbc/myoracle5" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@15.1.1.40:1521:dbastra" username="aradmin"
password="ar#admin#" maxActive="20" maxIdle="10" maxWait="-1" />

</Context>
</Host>
</Engine>
</Service>

for BOTH versions:

Here there are some points we should take a look:

<Context> in [server.xml] !!
"Please note that for tomcat 5, unlike tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. Instead, put them in the META-INF/context.xml directory of your WAR file or the conf directory as described above."
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

And if we cut the <Context>..</Context> body we described above from the server.xml and paste it into [META-INF/context.xml] or into [conf/Catalina/localhost/oracleTest.xml], it works...


<?xml version='1.0' encoding='utf-8'?>
<Context crossContext="true" docBase="/forge/oracleTest" path="/oracleTest" reloadable="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt" timestamp="true" />

<Resource ... (the one that applies to your version)>

</Context>

GlobalNamingResources:
"GlobalNamingResources element defines the global JNDI resources for the Server. These resources are listed in the server's global JNDI resource context. This context is distinct from the per-web-application JNDI contexts described in the JNDI Resources HOW-TO. The resources defined in this element are not visible in the per-web-application contexts unless you explicitly link them with <ResourceLink> elements."
http://tomcat.apache.org/tomcat-5.5-doc/config/globalresources.html
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Resource%20Links


Article From http://forums.sun.com/thread.jspa?threadID=567630&start=15

No comments: