tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mohammed Bin Mahmood" <>
Subject Problem closing datasource when used as JNDI resource
Date Wed, 19 Aug 2009 12:14:41 GMT
Hello All,

I am using datasource as a JNDI resource described in 
The problem I see is that datasource is not closed cleanly when the server
shuts down.
I looked into the code and I can only see that it destroys the Resource and
other references. But destroys or close the datasource on a whole.

I even tried to see by implementing
interface and found that event for before and after registration is called
but I do not find anything which un registers my datasource object.

In the threads discussed in below links, I found that tomcat do not closes
the datasource and it has to be closed on our own.
In my case, I use the datasource for the server instead for a specific web
I had to close it with a help of a listener after the server stops.

My Questions/problems are:
When tomcat takes care of creating maintaining datasource, then does it not
closes it when server is shut down?
Do we have to close it on our own (the way I do) or is there any better way
to do it without external listener?

Below are my configurations:

Server version: Apache Tomcat/5.5.20
Server built:   Sep 12 2006 10:09:20
Server number:
OS Name:        Windows XP
OS Version:     5.1
Architecture:   x86
JVM Version:    1.5.0_08-b03
JVM Vendor:     Sun Microsystems Inc.

In server.xml

  <Listener className=""

	<Resource auth="Container" 
		description="xyz Connection pool" 
		propertiesFile="" />

In web.xml

		<description>Resource references to database connection pool
configured in server.xml</description>

public class ServerLifecycleListenerImpl implements

	private String dsName;

	public void lifecycleEvent(LifecycleEvent event) {
		if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) {
			Object source = event.getSource();
			if (source instanceof StandardServer) {
			StandardServer server = (StandardServer)source;
			javax.naming.Context globalNamingContext =
				try {
					Object ds =
					if (ds instanceof BasicDataSource)
					else {
						throw new
SQLException("Cannot find Datasource");
				} catch (NamingException e) {
					//Logging code
				} catch (SQLException e) {
					//Logging code

	public void setDsName(String dsName) {
		this.dsName = dsName;

Mohammed Bin Mahmood.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message