context.xml

Notes

Purpose
context.xml is used by Ant when installing, reloading, removing, deploying, and undeploying AddressBook into Tomcat. It contains the details of AddressBook's "context" [the parameters that Tomcat uses when executing AddressBook].

This code defines the context path, the document base, the debug level. the reloadable flag, and the cross context flag:

    <Context path="/AddressBook" docBase="AddressBook" debug="1" reloadable="true" crossContext="true">

        ... Logger Definition goes gere ...

        ... DataSource Definition goes gere ...

    </Context>


Logger Definition
This code defines a Logger as a org.apache.catalina.logger.FileLogger class. An AddressBook log file will be created in Tomcat's logs directory named localhost_AddressBook_log.YYYY-MM-DD.txt where YYYY-MM-DD is the log's date:

    <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_AddressBook_log." suffix=".txt" timestamp="true"/>


DataSource Definition
This code defines a Resource named jdbc/Public as a javax.sql.DataSource class:

    <Resource name="jdbc/Public" auth="Container" type="javax.sql.DataSource"/>

This code defines the ResourceParams named jdbc/Public as a org.apache.commons.dbcp.BasicDataSourceFactory class:

    <ResourceParams name="jdbc/Public">

        <parameter>
            <name>factory</name>
            <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
        </parameter>

        ... the Database Parameters go here ...

        ... the Connection Pooling Parameters go here ...

    </ResourceParams>


Database Parameters

url
The database's URL, url = jdbc:mysql://localhost:3306/Public?autoReconnect=true. By default MySQL closes idle connections after 8 hours. If this happens, using autoReconnect=true ensures that Tomcat will automatically reconnect. Other databases may work differently.

    <parameter>
        <name>url</name>
        <value>jdbc:mysql://localhost:3306/Public?autoReconnect=true</value>
    </parameter>

driverClassName
The JDBC driver class name, driverClassName = org.gjt.mm.mysql.Driver which is the class name for the MySQL Connector/J JDBC driver.

    <parameter>
        <name>driverClassName</name>
        <value>org.gjt.mm.mysql.Driver</value>
    </parameter>

username
The MySQL username for database access, username = mysqlusername.

    <parameter>
        <name>username</name>
        <value>mysqlusername</value>
    </parameter>

password
The MySQL password for database access, password = mysqlpassword.

    <parameter>
        <name>password</name>
        <value>mysqlpassword</value>
    </parameter>


Connection Pooling Parameters

maxActive
The maximum number of connections in pool, maxActive = 100. Set maxActive = 0 for no limit. Make sure to configure max_connections large enough to handle all of your db connections.

    <parameter>
        <name>maxActive</name>
        <value>100</value>
    </parameter>

maxIdle
The maximum number of idle connections to retain in pool, maxIdle = 30. Set maxIdle = 0 for no limit.

    <parameter>
        <name>maxIdle</name>
        <value>30</value>
    </parameter>

maxWait
The maximum time to wait for a connection to become available [in milliseconds], maxWait = 10000. Set maxWait = -1 to wait indefinitely. If there is a timeout, an Exception is thrown.

    <parameter>
        <name>maxWait</name>
        <value>10000</value>
    </parameter>

removeAbandoned
If removeAbandoned = true then when available db connections run low the connection pool will recover and recyle any abandoned dB connections it finds. The default is removeAbandoned = false.

    <parameter>
        <name>removeAbandoned</name>
        <value>true</value>
    </parameter>

removeAbandonedTimeout
The number of seconds a dB connection has to be idle before it is considered abandoned by the connection pool, removeAbandonedTimeout = 300. removeAbandonedTimeout = 300 is the default time.

    <parameter>
        <name>removeAbandonedTimeout</name>
        <value>300</value>
    </parameter>

logAbandoned
If logAbandoned = true the connection pool will log a stack trace of the code that abandoned the dB connection resources.

    <parameter>
        <name>logAbandoned</name>
        <value>true</value>
    </parameter>


Complete Source Code


<Context path="/AddressBook" docBase="AddressBook" debug="1" reloadable="true" crossContext="true">

    <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_AddressBook_log." suffix=".txt" timestamp="true"/>

    <Resource name="jdbc/Public" auth="Container" type="javax.sql.DataSource"/>

    <ResourceParams name="jdbc/Public">

        <parameter>
            <name>factory</name>
            <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
        </parameter>

        <parameter>
            <name>url</name>
            <value>jdbc:mysql://localhost:3306/Public?autoReconnect=true</value>
        </parameter>

        <parameter>
            <name>driverClassName</name>
            <value>org.gjt.mm.mysql.Driver</value>
        </parameter>

        <parameter>
            <name>username</name>
            <value>mysqlusername</value>
        </parameter>

        <parameter>
            <name>password</name>
            <value>mysqlpassword</value>
        </parameter>

        <parameter>
            <name>maxActive</name>
            <value>100</value>
        </parameter>

        <parameter>
            <name>maxIdle</name>
            <value>30</value>
        </parameter>

        <parameter>
            <name>maxWait</name>
            <value>10000</value>
        </parameter>

        <parameter>
            <name>removeAbandoned</name>
            <value>true</value>
        </parameter>

        <parameter>
            <name>removeAbandonedTimeout</name>
            <value>300</value>
        </parameter>

        <parameter>
            <name>logAbandoned</name>
            <value>true</value>
        </parameter>

    </ResourceParams>

</Context>