tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mohammed Bin Mahmood" <moham...@sustainlane.com>
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
http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html 
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 javax.management.MBeanRegistration
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.
http://www.nabble.com/Right-way-to-close-database-connection-pool-td24832197
.html 

http://www.nabble.com/JNDI-datasource-failing-td4639851.html 
In my case, I use the datasource for the server instead for a specific web
app.
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:  5.5.20.0
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

<Server>
  <Listener className="com.xyz.foo.ServerLifecycleListenerImpl"
dsName="jdbc/myDB"/>
  <GlobalNamingResources>

	<Resource auth="Container" 
		description="xyz Connection pool" 
		name="jdbc/myDB" 
		type="javax.sql.DataSource"
		factory="com.xyz.foo.DataSourceFactory"
		propertiesFile="db.properties" />
  </GlobalNamingResources>
        ...
        ...
</server>

In web.xml

	<resource-ref>
		<description>Resource references to database connection pool
configured in server.xml</description>
		<res-ref-name>jdbc/myDB</res-ref-name>
  		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

Listener:
public class ServerLifecycleListenerImpl implements
org.apache.catalina.LifecycleListener{

	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 =
server.getGlobalNamingContext();
				try {
					Object ds =
globalNamingContext.lookup(dsName);
					if (ds instanceof BasicDataSource)
						((BasicDataSource)
ds).close();
					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;
	}
}



--
Thanks,
Mohammed Bin Mahmood.



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message