You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by TomazM <to...@arnes.si> on 2010/10/11 10:08:37 UTC

connection pool

Why if I reload application which use connection pool doesn't release connection's to MySQL DB?

Only if I restart Tomcat connection's are released, is this a bug.

I use:

Apache Tomcat/6.0.18  	1.6.0_16-b01  	Sun Microsystems Inc.  	Linux  	2.6.18-194.3.1.el5PAE  	i386


Regards, Tomaz


Re: connection pool

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tomaz,

On 10/11/2010 4:08 AM, TomazM wrote:
> Why if I reload application which use connection pool doesn't release connection's to MySQL DB?
> 
> Only if I restart Tomcat connection's are released, is this a bug.

Yes, it is: https://issues.apache.org/bugzilla/show_bug.cgi?id=22626

Remy has chosen not to fix this. I disagree with his reasons, but he's
the Tomcat dev, not me.

I wrote the ServletContextListener shown below to close-down my JNDI
DataSource when my app is taken out of service. I believe the DataSource
itself still hangs around after the webapp is stopped, but at least the
database connections should be closed.

Feel free to use this code however you want.

Configure it in web.xml like this:

<web-app>
...
    <context-param>
      <param-name>JNDIDataSourceName</param-name>
      <param-value>jdbc/myDataSource</param-value>
    </context-param>
...

    <listener>
        <listener-class>JNDIDataSourceShutdownListener</listener-class>
    </listener>
</web-app>

Here is the code:

import java.sql.SQLException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.sql.DataSource;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;

/**
 * A listener to shut down the JNDI DataSource created by Tomcat
 * (but not automatically cleaned up).
 *
 * @author Chris Schultz
 * @version $Revision: 1.1 $ $Date: 2009-08-06 16:42:32 $
 */
public class JNDIDataSourceShutdownListener
    implements ServletContextListener
{
    private String _dataSourcePath;

    public void contextInitialized(ServletContextEvent e)
    {
        ServletContext application = e.getServletContext();

        _dataSourcePath
            = application.getInitParameter("JNDIDataSourceName");

        if(!_dataSourcePath.startsWith("java:"))
            _dataSourcePath = "java:/comp/env/" + _dataSourcePath;
    }

    /**
     * Attempts to close the DataSource
     */
    public void contextDestroyed(ServletContextEvent e)
    {
        ServletContext application = e.getServletContext();

        try
        {
            Context ctx = new InitialContext();

            DataSource ds = (DataSource)ctx.lookup(_dataSourcePath);

            if(null == ds)
            {
                application.log(getClass().getName()
                                + ": No DataSource found at "
                                + _dataSourcePath);
            }
            else
            {
                application.log(getClass().getName()
                                + ": Closing DataSource "
                                + _dataSourcePath);

                close(ds, application);

                application.log(getClass().getName()
                                + ": Closed DataSource "
                                + _dataSourcePath);
            }
        }
        catch (NamingException ne)
        {
            application.log(getClass().getName()
                            + ": Unable to locate DataSource at "
                            + _dataSourcePath, ne);
        }
    }

    private void close(DataSource ds, ServletContext application)
    {
        try
        {
            Method closeMethod = ds.getClass().getMethod("close", null);

            if(null == closeMethod)
                throw new NoSuchMethodException(ds.getClass().getName()
                                                + ".close()");
            closeMethod.invoke(ds, null);
        }
        catch (Exception e)
        {
            application.log(getClass().getName()
                            + ": Cannot close DataSource", e);
        }
    }
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAky2AMEACgkQ9CaO5/Lv0PBE4ACgwLhTvCee8UAppLiljqlWLvyx
ErYAoLDHyFdoEIsW2riByd7ykuQNQ08R
=ZUk2
-----END PGP SIGNATURE-----

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


Re: connection pool

Posted by Pid <pi...@pidster.com>.
On 11/10/2010 09:08, TomazM wrote:
> Why if I reload application which use connection pool doesn't release connection's to MySQL DB?

Which connection pool and how is it defined?

Where is the JDBC driver placed, and which one (exact version) are you
using?

Can you upgrade to the latest version of Tomcat?


p

> Only if I restart Tomcat connection's are released, is this a bug.
> 
> I use:
> 
> Apache Tomcat/6.0.18  	1.6.0_16-b01  	Sun Microsystems Inc.  	Linux  	2.6.18-194.3.1.el5PAE  	i386
> 
> 
> Regards, Tomaz
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org