User Guide - Tomcat Installation and Tuning


Applicable Plans: Standard Plus VPS, Standard Max VPS, all Advanced VPS, all Premier VPS (not for Plesk or cPanel users)

Tomcat User Guide – Installation and Tuning

“Tomcat is the servlet container that is used in the official Reference Implementation for the Java Servlet and Java Server Pages technologies. The Java Servlet and Java Server Pages specifications are developed by Sun under the Java Community Process.- From the Apache Software Foundation Website”
http://tomcat.apache.org/index.html

The eApps Hosting service is designed to make it easy to use Tomcat for serving dynamic content in your Virtual Private Server (VPS). This User Guide is not intended as a reference source for Tomcat. See the Links to other information at the end of this document if you need detailed information about Tomcat.

The purpose of this User Guide is to explain how to install Tomcat in your VPS on the eApps Hosting service, and perform basic tuning if necessary. An overview of how to upload your Tomcat application on your VPS is also offered.

Instructions on how to deploy your Tomcat applications using the three supported approaches are found in their respective User Guides:


Please refer to the User Guide that references your deployment method of choice. The vast majority of eApps customers will need to use the Deployment with mod_jk and mod_proxy_ajp User Guide. The Deployment in Standalone Mode (without Apache) approach is generally only used when deploying an application that specifically needs that approach as a requirement.

If you follow the guidelines explained in these User Guides you will remain safely inside the eApps Support boundaries. If you choose to use a custom configuration that is outside of the guidelines contained in this document, you may be subject to service fees if you need assistance from the eApps Support department.

The Java libraries, Tomcat, and mod_jk are not available in the Standard plan due to memory requirements. The Standard Plus plan does have enough memory to run Tomcat and serves as our entry level plan for Tomcat users. However, it is important that you select a plan that has enough memory for your application during peak usage.

If you are a JBoss user, please do not use this Guide. Refer to the JBoss User Guide. Tomcat is bundled with JBoss and as such the User Guide for JBoss includes information to configure Tomcat.

Prerequisites 

Tomcat 
Java Libraries 
mod_jk 
mod_proxy_ajp 

How to Use Tomcat 

Understanding Tomcat configuration files 
How Tomcat handles requests 
Starting and stopping Tomcat 

How to upload your Tomcat applications 

Uploading application files – an overview 
Using SFTP 
Using the Control Panel 
Using the Tomcat Manager and Tomcat Administration tools (Tomcat 4/5 only) 

Three Approaches for Deploying Tomcat Applications

Overview of the three approaches for deployment 

Tuning Tomcat 

Set the Java Heap correctly 
Use Tomcat 6 and Java 6 
Keep Development Mode off 
Use Database Connection Pooling (DBCP) When Necessary 

Common Issues and Solutions 
Links to other information


Prerequisites

Tomcat requires that Tomcat and the Java Libraries are installed. Depending on how you are going to deploy your Tomcat application, you may also need to install the mod_jk module for Apache.

Tomcat

To use Tomcat you will have to install Tomcat from the Control Panel. Only one version of Tomcat is allowed, meaning that you can not install both Tomcat 5 and Tomcat 6 in your VPS. You also cannot install   both Tomcat and JBoss on your VPS. Unless your existing code base requires a specific version of Tomcat, it is recommended that you install the latest version of Tomcat available in your VPS.

To install Tomcat, follow these steps:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct Virtuozzo system.

Go to Applications, and click on the Add Application link. Select the box next to the version of Tomcat that you wish to install, and then scroll down and click the Next button.


This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The application should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.

Java 

Please install the newest version of Java  available to you unless your existing code base requires an older version.

To install Java , follow these steps:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct Virtuozzo Container.

Go to Applications, and click on the Add Application link. Select the box next to the version of Java you wish to install, and then scroll down and click the Next button. Newer plans will have either Java-SE-5 or Java-SE-6, older plans will have J2SE or Java6.

This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The application should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.

mod_jk

mod_jk is an Apache module that serves as a front end to Tomcat. mod_jk allows you to pass requests for Tomcat through Apache on port 80.

To install mod_jk, follow these steps:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct Virtuozzo container.

Go to Applications, and click on the Add Application link. Select the box next to mod_jk, and then scroll down and click the Next button.

This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The application should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.

mod_proxy_ajp

mod_proxy_ajp is only available on CentOS 5 plans. Because it is an Apache module, it is added as part of the Apache application when Tomcat is installed.


How to Use Tomcat

NOTE – in this User Guide, the X in TomcatX or tomcatX refers to the version of Tomcat you have installed. The domain of http://example.com is used as the default place holder domain to show how the URL or code example should look. You are expected to replace tomcatX and example.com with the correct version and domain for your specific setup.

Understanding Tomcat configuration files

There are four main configuration files for Tomcat. To edit these files, you will need root access to the VPS. You will also need to use a plain text editor, such as Notepad or vi. Do not use an application such as Word or Wordpad, because those applications insert control characters into the text that can cause problems with the Tomcat service starting. For information on how to access/download these files, see the SFTP and SSH User Guides.

NOTE – eApps uses the Java Service Wrapper to run Tomcat as a Linux daemon. This means that configurations that would normally go in the catalina.sh file go in the /etc/tomcatX/tomcat.conf file instead. If you put your configurations in catalina.sh, they will not be used by the Tomcat server. More information about the Java Service Wrapper, including links to the documentation, can be found here : http://wrapper.tanukisoftware.org/doc/english/introduction.html

Tomcat Configuration Files

The files server.xml, tomcat-users.xml and web.xml are in the /opt/tomcatX/conf directory. The tomcat.conf file is in the /etc/tomcatX directory.
  1. server.xml - This is the primary file used to configure the Tomcat server. This file can be edited manually or by using the Tomcat Administration tool (Tomcat 4/5 only).
  2. tomcat-users.xml - defines the user authentication info for accessing the Tomcat Administration and Tomcat Manager tools. You may create users and set passwords in this file.
  3. web.xml - defines settings for the Tomcat server such as error pages and the default index page. (These settings may also need to be set in Apache if you are using mod_jk or mod_proxy-ajp. See the eApps documentation for Apache for more info)
  4. tomcat.conf - located in /etc/tomcatX directory, this is the configuration file for the Java Service Wrapper. eApps uses the Java Service Wrapper to run Tomcat as a Linux daemon. This configuration file is used to set your parameters, such as the Java memory Heap, instead of using catalina.sh.

How Tomcat handles requests

Tomcat uses a Host and a Context to determine what application to serve. Upon installation, there is one Host already created, called 'localhost', which is also set as the default host. Since having a default host is mandatory, do not edit or remove the localhost entry. Instead, create new host for your applications as needed.

To Tomcat, http://example.com and http://www.example.com are seen as two completely different Host names, and each can serve completely different content. If you want your application to listen on both Hosts, you have to set one as the actual Host, and the other as an Alias in the same Host block. This is covered in the User Guides for the  Deployment approaches..

When Tomcat receives a request, it first looks to see if there is a Host configured that matches the domain name in the URL that was requested. It then looks inside each Host to see if there is an Alias that matches the domain name in the URL. If Tomcat doesn't find a matching Host block or a matching Alias inside a Host block, then it will serve the default Host block, which is localhost.

When Tomcat finds the correct Host, it then looks at the rest of the URL to get the path of the application and tries to match that with a Context inside the Host to determine what to serve. The Context will define what application to serve for a given path by defining a docBase for that path. This is covered in more detail in the User Guides for the Deployment approaches.

Starting and stopping Tomcat

Tomcat can be stopped or started from either the command line, the Control Panel, or the Tomcat Manager interface (if available).

Command line

Tomcat can be stopped, started or restarted from the command line. All these commands have to be run as the root user. Remember to substitute your version of Tomcat for tomcatX.

[root@example]# service tomcatX stop
[root@example]# service tomcatX start
[root@example]# service tomcatX restart
[root@example]# service tomcatX status

(the last command will show the current status of the Tomcat server)

Control Panel

Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct Virtuozzo container.

Go to Applications, and either click on the All Applications link and then the Tomcat link, or click directly on the application link for Tomcat (if available). There will be a radio button to either Stop or Start the Tomcat server, depending on its current status.

Tomcat can also be stopped or started from the Services link on the System Tab. Click on the link for Tomcat, and there will be a radio button to Stop or Start the Tomcat server, depending on its current status.

Tomcat Manager (Tomcat 4/5 only)

This tool also allows you to start and stop a particular application without stopping the entire Tomcat server. This application can be accessed at http://example.com:8080/manager/html or http://example.com/manager/html if mod_jk is installed or mod_proxy_ajp directives are in place. See the specific deployment examples for instructions on how to set this up.

How to upload your Tomcat applications

Uploading application files – an overview

To upload your Tomcat application, you must upload the files to the deployment directory that you have specified in your server.xml file, and then restart Tomcat. You can upload your files using the File Manager in the System Tab of the Control Panel if they are under 5 MB in size,  the Tomcat Manager or Tomcat Administration tools (Tomcat 4/5 only), or you can use SFTP to upload the files to the correct directory.

Once you have uploaded  your files, make sure they have the correct owner and group for their location.

Using SFTP

SFTP allows you to upload files of any size to your deployment directories, and is much faster than using the Control Panel. If you are going to be frequently uploading/changing/working with your Tomcat applications, SFTP is the best choice. For more information on SFTP, please see the SFTP User Guide.

Using the Control Panel

You can use the File Manager to upload your application to your deployment directory if the application is less than 5MB in size. To use the File Manager, login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct Virtuozzo container. Then, click on File Manager. Using the File Manager, navigate to the directory you are using for your Tomcat application files. Once you are in that directory, click on Upload File. In the Upload File screen, you can Browse for the file to be uploaded on your local computer, and also specify a New file name if necessary. You also can set the Permissions for the files, and the Ownership. Make sure you choose Permissions and Ownership that match the other files where you are uploading your applications. Once you have everything ready, click OK to upload your files.  
Once you have uploaded your Tomcat application files, restart Tomcat as per the instructions in Starting and Stopping Tomcat – Control Panel.

Using the File Manager in the Control Panel is the slowest and most restrictive way to upload your files. If you are going to be frequently working with your Tomcat application files, you will be better served by using SFTP to connect to your VPS and upload your files. For example, you cannot upload a file over 5 MB using the Control Panel, while there is no such restriction using SFTP. It is not uncommon for the WAR file for a complex Tomcat application to be larger than 5 MB, which means that you will not be able to use the Control Panel to upload that file.

Using the Tomcat Manager and Tomcat Administration tools (Tomcat 4/5 only)

Tomcat Manager tool
The Tomcat Manager tool can be used to deploy and undeploy an application as well as start and stop Tomcat applications without stopping and restarting the entire Tomcat server. The Tomcat Manager tool can be accessed at http://example.com:8080/manager/html or http://example.com/manager/html if mod_jk is installed or mod_proxy_ajp directives are in place.

You must first enable the Tomcat Manager application for your site. This can be done by simply adding its Context to your Host inside the server.xml file. If you have multiple Hosts defined in server.xml, and a Context for the Tomcat Manager application in each one, the Tomcat Manager application will only show those applications deployed in the Host used in the URL you used to access the Tomcat Manager application. The Context for the Tomcat Manager goes after your last application Context in server.xml.

To add the Tomcat Manager application Context to your server.xml file, you will either need to connect to the VPS via SSH as the root user and edit the file using a Linux text editor such as vi or vim, or download the server.xml file using SFTP to your local computer and edit it using a plain text editor, such as Notepad (not Word or Wordpad).

Once you have accessed the file, add the lines in BOLD to your existing Host block in server.xml, substituting your own domain name and version of Tomcat as necessary.


<Host appBase="/home/webadmin/example.com" name="example.com" autoDeploy="true">
<Alias>www.example.com</Alias>
<Context path="" docBase="html" debug="0" reloadable="true"/>
<!-- Manager application context below -->
<Context path="/manager" docBase="/opt/tomcatX/server/webapps/manager" privileged="true" debug="0">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="example.com._manager" suffix=".txt" timestamp="true"/>
</Context>

</Host>


Once you have restarted Tomcat, the Tomcat Manager tool will be available.

Tomcat Administration tool (Tomcat 4/5 only)
The Tomcat Administration tool can be used to edit the configuration parameters for your Tomcat applications after they have been deployed. The Tomcat Administration application can be accessed at http://example.com:8080/admin or http://example.com/admin if mod_jk is installed or mod_proxy_ajp directives are in place. Remember that the Tomcat Administration tool is not available on Tomcat 6 by default. You will need to install it manually.

You must first enable the Tomcat Administration tool for your site. This can be done by simply adding its Context to your Host inside the server.xml file. If you have multiple Hosts defined in server.xml, and a Context for the Tomcat Administration application in each one, the Tomcat Administration application will only show those applications deployed in the Host used in the URL you used to access the Tomcat Administration tool. The Context for the Tomcat Administration tool goes after your last application Context in server.xml.

To add the Tomcat Administration tool Context to your server.xml file, you will either need to connect to the VPS via SSH as the root user and edit the file using a Linux text editor such as vi or vim, or download the server.xml file using SFTP to your local computer and edit it using a plain text editor, such as Notepad (not Word or Wordpad).

Once you have accessed the file, add the lines in BOLD to your existing Host block in server.xml, substituting your own domain name and version of Tomcat as necessary.


<Host appBase="/home/webadmin/example.com" name="example.com" autoDeploy="true">
<Alias>www.example.com</Alias>
<Context path="" docBase="html" debug="0" reloadable="true"/>
<!-- Admin application context below -->
<Context path="/admin" docBase="/opt/tomcatX/server/webapps/admin" privileged="true" debug="0"/>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="example.com." suffix=".txt" timestamp="true"/>

</Host>

Once you have restarted Tomcat, the Tomcat Administration tool will be available.

A note regarding security for the Tomcat Manager and Administration Tools
The default user name and password for the Tomcat Manager and Administration applications is admin as the user and example.com as the password, where ‘example.com’ is the hostname of your VPS as shown in the System Tab of the Control Panel. You can change these by editing /opt/tomcatX/conf/tomcat-users.xml file and edit the line below with your desired user name and password.

<user user name="admin" password="example.com" roles="tomcat,manager,admin"/>

If you wanted to change the user name to ‘eapps’ and the password to ‘hosting’, you would change the line as follows:

<user user name="eapps" password="hosting" roles="tomcat,manager,admin"/>

You can specify separate users for different roles if desired.


Three Approaches for Deploying Tomcat Applications

Overview of the three approaches for deployment

Your eApps Virtual Private Server includes the Apache web server as part of the base environment. Apache is designed to serve static content, perl scripts, php scrips, and other types of content that requires a web server. Tomcat is designed primarily to serve JSP pages and servlets.

One of the decisions you will need to make when deploying your Tomcat application is how you want Tomcat and Apache to interact. In most cases you will want to use Apache as a front end to Tomcat. In some cases you may want to run Tomcat without Apache. This section explains three approaches for your deployment.


Before you proceed you will need to determine which of the three approaches below is appropriate for you. The three supported approaches for deployment are:

  • Using mod_jk – the most common way of configuring Tomcat and Apache. This approach is available in both CentOS 4 and CentOS 5 plans. This is the most common approach.
  • Using mod_proxy_ajp – similar to using mod_jk, in that you have to specify directives to forward requests from Apache to Tomcat. This approach is only available on CentOS 5 plans.
  • Using Tomcat in standalone mode – this approach should only be used if your application specifically requires this method. Generally, this approach is only used when your VPS does nothing but run a Tomcat application. In other words, it is not used for e-mail, or any other function other than the Tomcat application. This approach is for advanced users who are already very familiar with configuring Tomcat and Apache. The vast majority of Tomcat users do not need this approach.
For instructions on how to use these deployment approaches, please see the User Guide specific to that approach.

Tomcat User Guide – Deployment with mod_jk and mod_proxy_ajp
Tomcat User Guide – Deployment in Standalone mode (without Apache)


Tuning Tomcat

Set the Java Heap correctly

Java Heap Settings – an overview
The most important part of tuning Tomcat for better performance is to optimize the Java Heap settings. The Java Heap size is the amount of memory allocated to the Java Virtual Machine (JVM). The heap is where Java objects live, and there must be enough memory allocated to the JVM to support the needs of the deployed Tomcat applications.

The most common issue with Tomcat, by far, is a lack of available resources. In many cases, this is due to the Java Heap size either not being configured at all, or not being configured correctly. In other cases, the VPS is simply too small for the type of application being run on it. For example, while it is possible to run Tomcat on the Standard Plus or Standard Max plans, those plans do not have the resources for a true enterprise application or a site that receives a high volume of traffic.

How to find the correct Java Heap settings

By default Tomcat is configured to use a maximum of 64 MB of RAM allocated to the heap. This setting will have to be changed in order to correctly optimize Tomcat. To find the correct heap size , you will have to stop Tomcat, and use the Linux ‘free’ command to see the available memory, and then multiple that value by two, and then subtract that result from the total RAM available for your plan. This number will be roughly what you need to set for the maximum heap size.

Note – the following must be done from the command line of the VPS while logged in via SSH as the root user. Also remember to substitute your version of Tomcat for the X in tomcatX.

[root@example]# service tomcatX status
Tomcat Application Server is running (19626).
[root@example]# service tomcatX stop
Stopping Tomcat Application Server...
Waiting for Tomcat Application Server to exit...
Stopped Tomcat Application Server.
[root@example]# service tomcatX status
Tomcat Application Server is not running
[root@example]# free -m
             total       used       free     shared    buffers     cached
Mem:          800         250        550          0          0          0
-/+ buffers/cache:        250        550
Swap:            0          0          0

In this example, with Tomcat stopped, the VPS plus all other applications and services running consume 250 MB of RAM.

Multiply this value by two giving a total of 500, and subtract that from the total available RAM on the VPS for a value of 300 MB for the maximum heap.

Remember, this is just a rough estimate. You may find that you need to adjust this number up or down as necessary, depending on how your application is configured and what other services your VPS is running.

Generally, the maximum heap size should be just under half the amount of memory available to your VPS. You can of course adjust this value higher or lower, but you may have to experiment to determine the true optimum heap size. See the chart below for the eApps recommendations for the maximum heap size broken down by Plan.

This chart shows the recommended maximum heap size per plan.
Plan Allocated Memory for Plan Max Java Heap Size (do not exceed)
Standard Plus 288MB 128MB
Standard Max 432MB 192MB
Advanced 800MB 384MB
Advanced Plus 1200MB 576MB
Advanced Max 1600MB 768MB
Premier 2048MB 960MB
Premier Plus 2560MB 1152MB
Premier Max 4096MB 1700MB
Dedicated Servers start with the 'total memory -(free memory x 2)' formula and increase as needed



If your JVM runs out of heap space, the message java.lang.OutOfMemoryError may appear in the /opt/tomcatX/logs/catalina.out log file. This message lets you know that you need to either adjust your heap settings, consider upgrading your plan, or optimize your application to use less memory and resources.

You can also use the same ‘free -m’ command as shown above, but with Tomcat still running. This will show you if you are hitting memory limits in your plan. If your ‘free’ memory drops below 100 MB, that is a sign that you may be pushing the resource limits in your plan. If this is the case, you should monitor the memory usage for several days to see if that number is consistent, or was just an anomaly due to peak load condition.

Adjusting the Java Heap settings

To set the Java Heap for Tomcat you must modify the wrapper.java.initmemory and the wrapper.java.maxmemory settings in your /etc/tomcatX/tomcat.conf file. It is best to set the init parameter lower than the maximum parameter to ensure that Java does not initialize more memory than is actually needed. This will optimize memory for non-Java processes.

Note – the following must be done from the command line of the VPS while logged in via SSH as the root user. Also remember to substitute your version of Tomcat for the X in tomcatX.

[root@example ~]# cd /etc/tomcatX/
[root@example tomcatX]# vi tomcat.conf

Using a text editor (in this example the vi editor), edit the tomcat.conf file and change the settings below to your desired heap size values
The default values for the Java Heap Size are 64 MB for Initial and Maximum. This is the same no matter which plan you have.

# Initial Java Heap Size (in MB)
wrapper.java.initmemory=64
# Maximum Java Heap Size (in MB)
wrapper.java.maxmemory=64

In this example, the defaults are changed to 128 MB for the Initial Heap Size and 384 for the Maximum Heap Size

# Initial Java Heap Size (in MB)
wrapper.java.initmemory=128
# Maximum Java Heap Size (in MB)
wrapper.java.maxmemory=256

Save and exit the tomcat.conf file, and restart Tomcat for the new Heap settings to take effect.

Use Tomcat 6 and Java 6

Tomcat 6 and Java 6 are more efficient than their predecessors. If possible, we recommend that you upgrade to the latest version of each to take advantage of these efficiencies. Make sure that your existing codebase will support this upgrade.

Keep Development Mode off

For performance reasons, we have changed the default mode for Tomcat's JSP Development Mode setting to False. What this means is that changes to JSP pages will not be picked up automatically by Tomcat.

If you change a JSP page and do not see the updates, check your JSP Development Mode. If it is set to FALSE, you will have to delete the hostname directories in /opt/tomcatX/work/Catalina and restart Tomcat or delete the compiled JSP.

You can set Development Mode back to True, but we do not recommend this for a production environment due to the performance hit it incurs. However, if you want to change Development Mode to True for testing, here are the steps.

Note – the following must be done from the command line of the VPS while logged in via SSH as the root user. Also remember to substitute your version of Tomcat for the X in tomcatX.

First, make a backup of the web.xml file before making any changes.

[root@example ~]# cd /opt/tomcatX/conf
[root@example conf]# cp web.xml{,.back}
[root@example conf]# vi web.xml

Using a text editor (in this example the vi editor), edit the web.xml file and make these changes.

Change:
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>development</param-name>
<param-value>false</param-value>
</init-param>

to:
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>development</param-name>
<param-value>true</param-value>
</init-param>

and then save and exit the file, and restart Tomcat. Again, this method should not be used in a production environment. If you wish to do so, you do so at your own risk.

Use Database Connection Pooling (DBCP) When Necessary

Database Connection Pooling – an overview

Database connection pooling can reduce connection creation time and resource usage, especially on sites with a high load. The steps below can help you implement Database Connection Pooling for your application deployed on Tomcat.

Install the correct JDBC driver

Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Go to Applications, and click on the Add Application link. Select the box next to either MySQL-connecter-J or PostgreSQL-JDBC-Driver, and then scroll down and click the Next button.

This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The application should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.

Context Configuration
Add the lines below to your context configuration in your Host element in server.xml, in your application META-INF/context.xml,or in your  /opt/tomcatX/conf/Catalina/hostname/context.xml. The exact location will depend on how you have deployed your application.

For MySQL
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="myuser" password="mypasswd" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>

For PostgreSQL
<Resource name="jdbc/myDB" auth="Container"
type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/javatest"
username="myuser" password="mypasswd" maxActive="20" maxIdle="10"
maxWait="-1"/>

General Notes About the Parameters Used Above ( From the apache.org Tomcat Documentation)

  • 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 -1 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.
  • user name and password - MySQL dB user name and password for dB connections
  • driverClassName - Class name for the old mm.mysql JDBC driver is org.gjt.mm.mysql.driver.We recommend using Connector/J driver. Class name for the official MySQL Connector/J driver is com.mysql.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 closes the connection. By default mysqld closes idle connections after 8 hours.

web.xml Configuration
You now need to configure your application WEB-INF/web.xml file to use the database resource. In your web.xml file add a resource reference like the one below for both MySQL and PostgreSQL

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/myDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

There are several other things that can be done to tune Tomcat which are out of the scope of this documentation. Please see the Links to other information section for external resources.


Common Issues and Solutions

Question - Why am I not seeing the changes in my application after I made coding changes and redeployed my files?
Answer - Often when making updates or changes to your site, it appears as if Tomcat does not load the changes, even after restarting Tomcat or your entire VPS. One possible reason is because Tomcat keeps a cached version of your site in the /opt/TomcatX/work directory. If you make a change to your site, particularly a large site, and want to see the change take effect right away, delete the contents of the work directory, then restart Tomcat. Another possible reason is that the default configuration for Tomcat in the eApps environment is for Development Mode to be turned off.

Question - Why am I getting a "jdbc error"?
Answer - This is an indication that your application is having problems connecting to the database. Make sure your database server is installed and running. Make sure you have installed the JDBC driver, as explained in the user guide for the database server you are using. Make sure your jdbc connection string url is correct. Try to connect to the database with the user name and password you are using in your application.

Question - Why am I getting a "class not found error"?
Answer - This is an indication that you are trying to use a lib that is not in your class path. Check that the jar file is in /opt/tomcatX/common/lib or /opt/tomcatX/lib or in your application/WEB-INF/lib directory.

Question - Tomcat is not starting after I added a host. What can it be?
Answer - If Tomcat fails to start after you added a Host there is most likely a configuration error in your server.xml file. Check and make sure all tags are properly closed and that Tomcat has read access to the docbase of your application.

Question - Tomcat is not starting and never has, what am I doing wrong?
Answer - If Tomcat fails to start right from the beginning, you probably do not have the Java language application installed. You will see an error "cannot initialize JVM" in the catalina.out error file. Go to the All Applications area on the System tab of your control panel and make sure you have the J2SE (java 5) or Java 6 application installed in your VPS.


Links to other information

Tomcat Home Page: http://tomcat.apache.org/
Load testing: http://jakarta.apache.org/jmeter/
Apache 2.2 Documentation: http://httpd.apache.org/docs/2.2/
Mod_jk Home Page: http://tomcat.apache.org/connectors-doc/
eApps mod_proxy_balancer User Guide:http://support.eapps.com/hsp/mod_proxy_balancer
eApps Apache User Guide: http://support.eapps.com/hsp/apache
Connection Pool: http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

Comments

Please login to comment