How to setup a tomcat server with JDK and DB2 runtime client

Jephe Wu - http://linuxtechres.blogspot.com


Objective: Preparing a Linux db2 client environment with JDK and Tomcat
Environment: CentOS 5.4, IBM DB2 V8.1 JDK 1.4.2 and Tomcat 4.1


Steps:

1. Preparing JDK environment:
cd /usr/local
./j2sdk-1.4.2xxx.bin
ln -sf j2sdk1.4.2xxx jdk

Put the following to /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/jdk
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib

then run Chmod +x java.sh

2. Preparing Tomcat environment:

# cd /usr/local/
# tar xvpfz /usr/local/src/jakarta-tomcat-4.1.31.tar.gz
# ln –sf  jakarta-tomcat-4.1.31 tomcat

Add tomcat user and group
# groupadd tomcat
# useradd –g tomcat –c “Tomcat User” –d /usr/local/tomcat tomcat
# chown –R tomcat:tomcat Jakarta-tomcat-4.1.31
# chown tomcat:tomcat tomcat


Put the following to /usr/local/tomcat/.bash_profile and give it 755 permission

CATALINA_HOME=/usr/local/tomcat
LD_LIBRARY_PATH=/home/db2inst1/sqllib/lib
LIBPATH=/home/db2inst1/sqlib/lib
DB2INSTANCE=db2inst1
export CATALINA_HOME DB2INSTANCE  LD_LIBRARY_PATH LIBPATH

Modify catalina.sh, to add this:
JAVA_OPTS='-Xmx1024m -XX:+AggressiveHeap' at the top of the file

use cronolog to auto rotate log daily
[jephe@app tomcat]$ grep -A 3 -B 3 cronolog bin/catalina.sh
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start | /usr/local/sbin/cronolog "$CATALINA_BASE"/logs/%Y-%m-%d.catalina.out \
      >> /dev/null 2>&1 &

      if [ ! -z "$CATALINA_PID" ]; then
          echo $! > $CATALINA_PID
      fi
  fi
note: you  can vi bin/catalina.sh , then search for 'stop' string, before the following line , you can add above cronolog filter
elif [ "$1" = "stop" ] ; then

3. Preparing DB2 Client

download IBM DB2 runtime client from www.ibm.com, login as root to install runtime client software
# cd /root
# tar xvf FP8_M00099.tar
# cd rtcl
#./db2_install (db2setup needs GUI , so use db2_install instead), it will install all  rpms to /opt/IBM/ directory.

create instance (CLI)
# cd /opt/ibm/db2/V8.1/instance
# groupadd  db2grp1
# useradd –c ‘DB2 Instance User’ –g db2grp1 –m db2inst1
# ./db2icrt –s client db2inst1  (important, even installation of rtcl sometimes created /home/db2inst1/sqllibxx for you, rename that, run this command as root)

# su - db2inst1
# db2 catalog tcpip node db1 remote 10.0.3.2 server 50000
note: node name cannot use -, _ is allowed
# db2 catalog database DB1 [ as DB1ALIAS ] at node db1
# db2 list db directory
# db2 list node directory
# db2 connect to DB1ALIAS user jephe using password

# db2 uncatalog node db1
# db2 uncatalog db DB1

 Set environment for db2inst1 user
Append the following into /home/db2inst1/.bash_profile
. /home/db2inst1/sqllib/db2profile

note: testing db2 runtime client first before using tomcat application:

db2 connect to db1 user schemanmame

db2 list tables for all

if you encounter errors like SQL0805N package "NULLID.SQLxxxxxxxxx" was not found. 
try to run 'db2 ? SQL0805N' to follow the suggestion below to bind.

db2 bind @db2ubind.1st blocking all grant public 



4. Setup tomcat again after finishing db2 client setup

Copy connector over
# cd /opt/IBM/db2/V8.1/java
# cp –i  db2java.zip /usr/local/tomcat/common/lib/db2java.jar
note:  you have to copy db2java.zip from db2 runtime client to above tomcat folder which is from tomcat server itself

# chown tomcat:tomcat /usr/local/tomcat/common/lib/db2java.jar


5. Preparing Firewall

Allow port 8080, 8443 and 8009 in /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT –m state –state NEW –m tcp –p tcp –dport 8080 –j ACCEPT
-A RH-Firewall-1-INPUT –m state –state NEW –m tcp –p tcp –dport 8443 –j ACCEPT
-A RH-Firewall-1-INPUT –m state –state NEW –m tcp –p tcp –dport 8009–j ACCEPT

# service iptables restart


6. Make tomcat listening on port 80
This section is referred from http://www.klawitter.de/tomcat80.html (How to run Tomcat on Port 80)

There are a few ways to make tomcat to be listening on port 80, you can run tomcat as root which is not recommended, anther way is to remain tomcat to listen on port 8080, and use iptables to forward port 80 request to tomcat:

steps:
iptables -t nat -A PREROUTING -d your hostname -p tcp --dport 80 -j REDIRECT --to-ports 8080
iptables-save > /etc/sysconfig/iptables
chkconfig iptables on

Under some circumstances, the HttpConenctor class reports the original port back to the client. Further requests will continue with that port (which is not the desired effect and might even be blocked by your firewall).

Besides switching to a more contemporary Connector like CoyoteConenctor (recommended), you can circumvent that problem by adding a proxyPort to the HttpConnector declaration:
<Connector
  className="org.apache.catalina.connector.http.HttpConnector"
  port="8080"
  proxyPort="80"
>
 
7. FAQ
a. if testing db2 connection got error like 'SQL0805N Package dc2j.NULLID.SQLC2D01.4141414141350 not found.', you can solve it by binding:
login as db2inst1 on db2 client / tomcat server, run:

db2 connect to db1 user db2inst1 (must login as db2inst1, not user)

cd sqllib/bnd
db2 bind   @db2cli.lst 

db2 bind   @db2bind.lst (may not have this filename, then just ignore this line)