Tomcat, installation / configuration 6.0.18 with Sun, connection with Oracle db.

Never worked before with Tomcat, but an application-release needed to be upgraded from the old Tomcat 4.x to a higher level. So why not 6.0.18 with the latest Java, on Suse 10.1, 64-bits. How hard can it be?

Somewhat different than Oracle application server, but what the hack. With thanks to Timo Schijf, developer.

1. Download Tomcat 6.0.18 : http://tomcat.apache.org/download-60.cgi

– Core (tar.gz)

– Deployer (tar.gz) don’t know why yet, may come in convenient.

2. Unpack/install Tomcat (user Oracle):

– gunzip apache-tomcat-6.0.18.tar.gz AND “tar xvf apache-tomcat-6.0.18.tar” , instantly sub-directory created: apache-tomcat-6.0.18

– gunzip apache-tomcat-6.0.18-deployer.tar.gz AND “tar xvf apache-tomcat-6.0.18-deployer.tar” instantly sub-directory created: apache-tomcat-6.0.18-deployer

Now we need Java. Could have picked the existing one, 1.4.2 in an Oracle-home, or for an Java-RPM from Suse Linux.

Chosen for an independent installation – Sun -, so I don’t have to mess with other existing versions of Java on this system.

3. Download Sun-Java 6.11 or 6.13: http://www.java.com – in this case the 64-bits self-extracting file.

Install 6.1X ==> chmod a+x jre-6u11-linux-x64-rpm.bin AND ./jre-6u11-linux-x64-rpm.bin (and answer ‘yes’ for the terms)

Will be installed in directory /usr/java –> ls -al /usr/java/jre1.6.0_11  or /usr/java/jre1.6.0_13

Modify the Tomcat startup / shutdown file ( apache-tomcat-6.0.18/bin/startup.sh and ../shutdown.sh):

Put somewhere in top:

– for 6.11 : JAVA_HOME=/usr/java/jre1.6.0_11

export JAVA_HOME

– for 6.13 : JRE_HOME=/usr/java/jre1.6.0_13

export JRE_HOME

start it : ./start.sh The output must look something like this:

~/apache-tomcat-6.0.18/bin> ./startup.sh

Using CATALINA_BASE: /home/oracle/apache-tomcat-6.0.18

Using CATALINA_HOME: /home/oracle/apache-tomcat-6.0.18

Using CATALINA_TMPDIR: /home/oracle/apache-tomcat-6.0.18/temp

Using JRE_HOME: /usr/java/jre1.6.0_11

Going down, like this:

~/apache-tomcat-6.0.18/bin> ./shutdown.sh

Using CATALINA_BASE: /home/oracle/apache-tomcat-6.0.18

Using CATALINA_HOME: /home/oracle/apache-tomcat-6.0.18

Using CATALINA_TMPDIR: /home/oracle/apache-tomcat-6.0.18/temp

Using JRE_HOME: /usr/java/jre1.6.0_11

Running processes:

~/apache-tomcat-6.0.18/conf> ps -ef |grep catal

oracle 1962 1 0 12:29 pts/3 00:00:00 /usr/java/jre1.6.0_11/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/home/oracle/apache-tomcat-6.0.18/conf/logging.properties -Djava.endorsed.dirs=/home/oracle/apache-tomcat-6.0.18/endorsed -classpath :/home/oracle/apache-tomcat-6.0.18/bin/bootstrap.jar -Dcatalina.base=/home/oracle/apache-tomcat-6.0.18 -Dcatalina.home=/home/oracle/apache-tomcat-6.0.18 -Djava.io.tmpdir=/home/oracle/apache-tomcat-6.0.18/temp org.apache.catalina.startup.Bootstrap start

Real check:

http://<nodename><domain:8080 –> you should see the main page of tomcat, with on-board documentation.

Default port = 8080, defined in <tomcat-home>/conf/server.xml. Looks like: port=”8080″ protocol=”HTTP/1.1″

4. Jdbc driver: as we connect to Oracle, we need a JDBC-driver – put Ojdbc14.jar in the directory apache-tomcat-6.0.18/lib.

5. Connection pooling. To configure this, you may put this in the /META-INF/context.xml file.

5.1 Example context.xml:

<?xml version=”1.0″ encoding=”UTF-8″?>

<Context path=”/myapp”>

<!– maxActive: Maximum number of dB connections in pool. Make sure you

configure your mysqld max_connections large enough to handle

all of your db connections. Set to 0 for no limit.

–>

<!– maxIdle: Maximum number of idle dB connections to retain in pool.

Set to -1 for no limit.  See also the DBCP documentation on this

and the minEvictableIdleTimeMillis configuration parameter.

–>

<!– maxWait: 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.

–>

<!– username and password: dB username and password for dB connections  –>

<!– driverClassName: Class name for the  JDBC driver.

–>

<!– url: The JDBC connection url for connecting to your MySQL dB.

The autoReconnect=true argument to the url makes sure that the

mm.mysql JDBC Driver will automatically reconnect if mysqld closed the

connection.  mysqld by default closes idle connections after 8 hours.

–>

<!– logAbandoned, removeAbandoned, removeAbandonedTimeout: Control whether

the container will prevent connection pool leaks by automatically removing

Connection, Statement, ResultSet, etc. objects that haven’t been closed

–>

<Resource name=”myDatabaseLink” auth=”Container”

maxActive=”100″ maxIdle=”30″ maxWait=”10000″

username=”<username>” password=”<password>”

driverClassName=”oracle.jdbc.driver.OracleDriver”

<!–url=”jdbc:mysql://localhost:3306/veilingdb?autoReconnect=true”

url=”jdbc:oracle:thin:@<domain>:1521:<dbname>?autoReconnect=true”

logAbandoned=”true” removeAbandoned=”true”

removeAbandonedTimeout=”60″ type=”javax.sql.DataSource” />

</Context>

5.2 Create a “Connection Pool”-class by the developer

Name of the datasoure must match the name in de context.xml above (here: myDatabaseLink).

Example:

package db;

import java.sql.Connection;

import java.sql.SQLException;

import javax.naming.InitialContext;

import javax.sql.DataSource;

public class ConnectionPool {

private static ConnectionPool pool = null;

private static DataSource dataSource = null;

public static synchronized ConnectionPool getInstance() {

if (pool == null) {

pool = new ConnectionPool();

}

return pool;

}

private ConnectionPool() {

try {

InitialContext ic = new InitialContext();

dataSource = (DataSource)ic.lookup(“java:/comp/env/myDatabaseLink”);

} catch (Exception e) {

e.printStackTrace();

}

}

public Connection getConnection() {

try {

return dataSource.getConnection();

} catch (SQLException e) {

e.printStackTrace();

return null;

}

}

}

5.3 To avoid problems with open connections the developer has to close the Resultset, statement and connection in the code after use.

Example how to use this in the code:

public static ArrayList<String> getArtikel(int ..Id) throws Exception {

ArrayList<String> row = new ArrayList<String>();

ResultSet rs = null;

PreparedStatement ps = null;

try {

ConnectionPool pool = ConnectionPool.getInstance();

Connection con = pool.getConnection();

ps =

con.prepareStatement(“select * from .. where id = ?”);

artikel.setInt(1, ..Id);

rs = artikel.executeQuery();

while (rs.next()) {

row.add(String.valueOf(rs.getInt(1)));

}

if (rs != null)

rs.close();

if (ps != null)

ps.close();

if (con != null)

con.close();

} catch (SQLException sqlex) {

System.out.println(“Fout in artikelen : ” + ” / ” + sqlex);

sqlex.printStackTrace();

System.exit(1);

} finally {

try {

// Close the resultset, statement and connection objects.

if (rs != null)

rs.close();

if (ps != null)

ps.close();

if (con != null)

con.close();

} catch (Exception ex) {

System.out.println(“Exception encountered: ” +

ex.getMessage());

ex.printStackTrace();

}

}

// Return the arraylist.

return row;

}

6. Deploying.

Most simple deployment method: make a .war-file and put it in /webapps:

6.1 Make a .war-file in Jdeveloper.

Go to the Resources-folder of the project.

Right click on webapp.deploy

Choose option ‘deploy to WAR-file’

6.2 Make a .war-file in Eclipse.

Go to the web-project-folder

Right click on the folder.

Choose export.

Choose WAR-file en ‘next’

Put this in the directory /webapps from Tomcat. Tomcat will create a folder with the name of the project and unpack the .war-file.