Archives

When using Tomcat to host and serve your web applications, the default configuration uses an in memory session store. This is a decent selection as a default but does have some problems. The first being that if you have many sessions being created you may consume more memory than you expect and more than you have allocated to Tomcat. It would be nice if the session store could be configured to swap out inactive sessions to a persistent store which would help mitigate this risk. The default configuration also will not persist the sessions if Tomcat is restarted (though this is configurable) which means any active sessions will be destroyed which could be jarring for users. If you are using Tomcat in a cluster, persisting sessions to disk will not be very valuable as another instance of Tomcat within the cluster will not be able to pick up and satisfy requests against these sessions. Luckily we can configure the session management semantics used by Tomcat to persist session data to a database or to the filesystem. In the rest of this post, I will describe how to quickly setup Tomcat to use the JDBCStore Session Manager component so that session data is persisted to your database when Tomcat is restarted or when a session has been idle for too long. Depending on your traffic patterns this may allow a single Tomcat instance to satisfy more sessions simultaneously and also may allow another instance of Tomcat to satisfy requests when one Tomcat instance is brought down in a controlled manner.

To get started, we will need to create a table in our database which is capable of storing session information. You can do so with a sql statement like the following.

Once your database has a table provisioned for storing session data, the next step is configuring Tomcat to use it to store the session data.

Add JDBC driver to Tomcat's classpath

This can be done by simply dropping the JDBC .jar something like mysql-connector-5.1.13.jar into the lib directory within the Tomcat installation directory /usr/share/tomcat6/lib/ is a popular location but may be different depending on your installation.

Configure Tomcat to Use the JDBCStore

Within your .war file which is being deployed, you can include a file META-INF/context.xml, which allows you to provide application context configuration. Adding a section such as the following will instruct Tomcat to store session information to the database for
this context.

Deploy Your Application

Now when you deploy your application using whatever process you have in place, session information should be stored in the database. Note that session information is only stored to the database when Tomcat is stopped or when a session has been inactive for a specified period of time so you may not see entries in the table immediately.

Conclusion

Hopefully this article has provided you with enough information to configure your Java based web application to use the JDBCStore for session persistence. If you have any problems getting this setup, feel free to leave a comment. The references also have information on how to configure Tomcat including more specific configuration parameters.