Tomcat Context.xml File Deletion Issue: Causes and Solutions for Developers


1 views

During Java web application development, many of us have encountered Tomcat unexpectedly deleting our carefully configured context.xml files. This typically occurs when the file is placed in /etc/tomcat6/Catalina/localhost/ (or equivalent path for your Tomcat version).

Tomcat's automatic deletion of context files stems from its deployment architecture:

  • Auto-deployment feature: Tomcat monitors the conf/Catalina/localhost directory for changes
  • Resource management: It cleans up what it considers temporary or obsolete configurations
  • Hot deployment conflicts: When reloading applications, Tomcat may remove and recreate context files

From community reports and personal experience, these scenarios trigger the deletion:

1. Application redeployment
2. Tomcat server restart
3. Modification of related web.xml files
4. Changes in the WAR file structure

Option 1: Use the conf/context.xml file

<Context>
    <Resource name="jdbc/myDB" auth="Container"
              type="javax.sql.DataSource"
              username="dbuser"
              password="dbpass"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/mydb"/>
</Context>

Option 2: Embed context in server.xml

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="false">
    <Context path="/owners" docBase="/path/to/your/app" >
        <!-- Your resource definitions here -->
    </Context>
</Host>

Option 3: Context fragment in META-INF

Place your context.xml in src/main/webapp/META-INF/context.xml (for Maven projects):

<Context path="/owners" reloadable="false">
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <!-- Other configurations -->
</Context>

For complete control, modify Tomcat's deployment settings in conf/server.xml:

<Host name="localhost" appBase="webapps"
      unpackWARs="true" autoDeploy="false"
      deployOnStartup="false">
    <!-- Your context configurations -->
</Host>

To track when and why deletions occur, add this JVM parameter:

-Dorg.apache.catalina.startup.ContextConfig.jmxEnabled=true

Then monitor via JMX using JConsole or similar tools.

  • Set file permissions to read-only after creation (chmod 444)
  • Use symbolic links to a protected directory
  • Implement a FileWatcher service to restore deleted files automatically

If you've verified the file deletion occurs during runtime with:

  • No configuration changes
  • No application redeployment
  • No Tomcat restarts

File a bug with your Tomcat version and exact reproduction steps at Apache Bugzilla.


As a Java developer working with Tomcat, you've probably encountered this frustrating scenario: You carefully configured your context.xml file (or in this case, owners.xml) in /etc/tomcat6/Catalina/localhost/, only to find Tomcat has deleted it without warning. This behavior isn't just annoying - it can seriously disrupt your development workflow.

Through debugging and community reports, we've identified several triggers:

  • Tomcat's auto-deployment feature scanning the configuration directory
  • Application reloading or redeployment events
  • Changes detected in the WAR file or application directory
  • Internal Tomcat cleanup processes

Tomcat manages context files dynamically, and this behavior is actually by design in certain scenarios:

// Tomcat's standard behavior for context files
if (autoDeploy && contextFile.exists()) {
    // Remove context file if corresponding app is removed
    if (!appBase.exists()) {
        contextFile.delete();
    }
}

The key factors causing your file deletion:

  1. Auto-deployment: When enabled, Tomcat monitors and manages context files automatically
  2. File location: Placing files in Catalina/localhost/ makes them subject to Tomcat's management
  3. Permission issues: Tomcat might recreate files with different permissions

1. Move to a Permanent Location

Place your context file in Tomcat's conf directory instead:

/etc/tomcat6/conf/Catalina/localhost/owners.xml

Files here are treated as static configuration and aren't subject to automatic deletion.

2. Disable Auto-Deployment

Modify your server.xml:

<Host name="localhost" appBase="webapps"
    unpackWARs="true" autoDeploy="false">

3. Use Context Fragments

Create a fragment in conf/context.xml:

<Context path="/owners" reloadable="false">
    <!-- Your configuration here -->
</Context>

4. File Permissions Solution

Make the file read-only for Tomcat:

chmod 444 /etc/tomcat6/Catalina/localhost/owners.xml

If you absolutely must keep the file in the original location and Tomcat keeps deleting it, consider this watchdog script:

#!/bin/bash
while true; do
    if [ ! -f "/etc/tomcat6/Catalina/localhost/owners.xml" ]; then
        cp /path/to/backup/owners.xml /etc/tomcat6/Catalina/localhost/
        systemctl restart tomcat6
    fi
    sleep 60
done

This checks every minute and restores the file if missing.

While this behavior can be frustrating, understanding Tomcat's design philosophy helps. The server prioritizes consistency between deployed applications and their configuration. By either working with this system (using recommended locations) or explicitly disabling automatic features, you can maintain stable development environment.

Remember that this issue has been reported in various Tomcat versions, so checking the Tomcat 6 documentation for your specific version might reveal version-specific solutions.