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.
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.
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.