TomcatExpert

New closeMethod for JNDI Resources

posted by mthomas on August 17, 2011 07:46 AM

Apache Tomcat 7 contains a number of new features around database connection pooling, which help administrators keep their application available and serving content, collecting customer information, and supporting their applications. The main one that has garnered a lot of attention is the new JDBC Connection Pool feature introduced by Filip Hanik last year. Another connection pool attribute not yet discussed here on TomcatExpert.com is the new closeMethod for speeding up the closing of JNDI resources that would otherwise be closed during garbage collection.

A Specific Requirement

This feature stemmed from a common challenge for applications that make use of database connection pools attached to databases with limited connections allowed. For instance, if a database allows 50 connections, and an application uses a connection pool of 50 connections, in theory everything should work. However, if the application is reloaded, the existing open connections in the old pool would not close until they have been garbage collected. So the new application connection pool while configured for 50 connections, could run out at 37 if the old connection pool had 13 connections open.

A General Solution

One solution to ensure your active connection pool always has connections available to it is to run garbage collection regularly, or each time an application is restarted. However, forcing a full garbage collection is not a particularly elegant solution to this problem and may cause problems of its own. To address this issue, a new attribute has been added for JNDI resources called closeMethod which allows the system administrator to define a zero-argument method call that allows Tomcat to call on a singleton resource when that resource is no longer required. If this attribute is defined, the method is called when the application is stopped, thus speeding up the clean-up of resources that would otherwise have waited to be cleaned up as part of garbage collection.

For more information on the new closeMethod, please see the section on context.html resource definitions in the official Apache Tomcat documentation.

Mark Thomas is a Senior Software Engineer for the SpringSource Division of VMware, Inc. (NYSE: VMW). Mark has been using and developing Tomcat for over six years. He first got involved in the development of Tomcat when he needed better control over the SSL configuration than was available at the time. After fixing that first bug, he started working his way through the remaining Tomcat bugs and is still going. Along the way Mark has become a Tomcat committer and PMC member, volunteered to be the Tomcat 4 & 7 release manager, created the Tomcat security pages, become a member of the ASF and joined the Apache Security Committee. He also helps maintain the ASF's Bugzilla instances. Mark has a MEng in Electronic and Electrical Engineering from the University of Birmingham, United Kingdom.

Comments

HI

Could you post a simple of how a set zero-argument method singleton call?

Fernando Franzini - Java Blog

Configuration example

The example below assumes that, like the re-packaged DBCP included with Tomcat, that the DataSource has a close() method that takes no arguments.

<Context>
...
<Resource name="jdbc/EmployeeDB" auth="Container"
type="javax.sql.DataSource"
description="Employees Database for HR Applications"
closeMethod="close"
...
/>
</Context>

Hi thomas

So....When I configured closeMethod="close" the tomcat will close dataSource during undeploy, stop and hot-deploy (update war)?

Fernando Franzini - Java Blog

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.