1. Shut down Tomcat
- Run
bin/shutdown.sh
orbin/shutdown.bat
to bring Tomcat down while you are making these changes. - Make a backup of your
server.xml
andcontext.xml
files in<tomcat-install>/conf/
so that you can easily revert if you have a problem.
2. Install the Drivers
- Download the MySQL Connector/J JDBC driver from http://dev.mysql.com/downloads/connector/j/
- After unpacking the file you have downloaded, you will find a file called something like
mysql-connector-java-5.1.22-bin.jar
. - If you are running Tomcat 7 or Tomcat 6, copy the file into
<tomcat-install>/lib.
On Tomcat 5.5, copy this file into thecommon/lib
directory of your Tomcat installation.
3. Configure Tomcat
Edit
<tomcat-install>/conf/context.xml
.
If you are running Tomcat 5.5 you edit<tomcat-install>/conf/server.xml
.Within the
Context
tags, insert the DataSourceResource
tag:<Resource name="jdbc/TermWebDS"
auth="Container"
type="javax.sql.DataSource"
username="termwebuser"
password="termwebpassword"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/termweb?autoReconnect=true&useUnicode=true&characterEncoding=utf8"
maxActive="20"
maxIdle="10"
validationQuery="Select 1"/>- Replace the username and password parameters with the correct values for your database.
- In the url parameter, replace the word 'termweb' with the name of the database your TermWeb data will be stored in.
Notes
- Why is the validationQuery element needed? When a database server reboots, or there is a network failure, all the connections in the connection pool are broken and this normally requires an application server reboot.
However, the Commons DBCP (Database Connection Pool) which is used by the Tomcat application server can validate connections before issuing them by running a simple SQL query, and if a broken connection is detected, a new one is created to replace it. To do this, you will need to set the "validationQuery" option on the database connection pool.
- The configuration properties for Tomcat's standard data source resource factory (org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory) are as follows:
- driverClassName – Fully qualified Java class name of the JDBC driver to be used.
- maxActive – The maximum number of active instances that can be allocated from this pool at the same time.
- maxIdle – The maximum number of connections that can sit idle in this pool at the same time.
- maxWait – The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception.
- password – Database password to be passed to our JDBC driver.
- url – Connection URL to be passed to our JDBC driver. (For backwards compatibility, the property driverName is also recognized.)
- user – Database username to be passed to our JDBC driver.
- validationQuery – SQL query that can be used by the pool to validate connections before they are returned to the application. If specified, this query MUST be an SQL SELECT statement that returns at least one row.
4. Configure the TermWeb Web Application
This step is only necessary if you for some reason need to use another data source name than the default jdbc/TermWebDS. In this case you will have to reapply these changes after every upgrade or reinstall of TermWeb.
- Edit
<tomcat-install>/webapps/termweb/WEB-INF/web.xml
. Go to the end of the file and locate the following element just before
</web-app>
:<resource-ref>
...
<res-ref-name>jdbc/TermWebDS</res-ref-name>
...
</resource-ref>- Change jdbc/TermWebDS to your desired data source name.
- Use the same data source name in the file you edited in the previous step
5. Restart Tomcat
Run bin/startup.sh
or bin/startup.bat
to start Tomcat and TermWeb with the new settings.