You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by John Zoetebier <jo...@transparent.co.nz> on 2003/11/09 02:41:13 UTC

DBCP question: what is closing the actual database connection ?

I have DBCP example code ManualPoolingDriverExample working just fine.
To test if the connection is indeed pooled I made a loop which opens and 
closes the connection.
The first connection takes about 2500 ms.
Next connections take 1 ms.
This looks pretty good to me.

My worry though is what happens if I shutdown the application.
What is actually closing down the real database connection stored in the 
DBCP pool ?
Do I have to call a method to indicate that the program is shuttinf down ?

-- 
John Zoetebier
Web site: http://www.transparent.co.nz

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: DBCP question: what is closing the actual database connection ?

Posted by Dirk Verbeeck <di...@pandora.be>.
You can get source from CVS or download a nightly build.
If you wait a couple of hours the 20031111 build should contain the new method.

Links to the CVS & download areas can be found here:
http://jakarta.apache.org/commons/dbcp/downloads.html

-- Dirk

John Zoetebier wrote:
> On Mon, 10 Nov 2003 15:05:27 +0100, Dirk Verbeeck 
> <di...@pandora.be> wrote:
> 
>> The PoolingDriver currently doesn't have a closePool method.
>> I have made an enhancement request for it
>> http://issues.apache.org/bugzilla/show_bug.cgi?id=24562
>> and will commit a fix for it in a few minutes.
> 
> 
> Ah, that is good news !
> Do I have to get the latest version from CVS ?
> How do I know I have the latest version that includes the fix ?
> 
>>
>> In your case (close at program shutdown), the real database driver 
>> will probably close the connections before the VM shutdown.
> 
> 
> I will double check this with McKoi mailing list.
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: DBCP question: what is closing the actual database connection ?

Posted by John Zoetebier <jo...@transparent.co.nz>.
On Mon, 10 Nov 2003 15:05:27 +0100, Dirk Verbeeck 
<di...@pandora.be> wrote:

> The PoolingDriver currently doesn't have a closePool method.
> I have made an enhancement request for it
> http://issues.apache.org/bugzilla/show_bug.cgi?id=24562
> and will commit a fix for it in a few minutes.

Ah, that is good news !
Do I have to get the latest version from CVS ?
How do I know I have the latest version that includes the fix ?

>
> In your case (close at program shutdown), the real database driver will 
> probably close the connections before the VM shutdown.

I will double check this with McKoi mailing list.

-- 
John Zoetebier
Web site: http://www.transparent.co.nz

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: DBCP question: what is closing the actual database connection ?

Posted by Dirk Verbeeck <di...@pandora.be>.
The PoolingDriver currently doesn't have a closePool method.
I have made an enhancement request for it
http://issues.apache.org/bugzilla/show_bug.cgi?id=24562
and will commit a fix for it in a few minutes.

In your case (close at program shutdown), the real database driver will probably 
close the connections before the VM shutdown.

-- Dirk

John Zoetebier wrote:

> On Sun, 9 Nov 2003 03:56:15 +0200, Schalk <sc...@volume4.co.za> wrote:
> 
>> John
>>
>> In basic situation you will have:
>> Class.forName(DRIVER);
>> con = DriverManager.getConnection(URL,USER,PASS);
>>
>> At the end you will have:
>> finally {
>>     try {   
>>     if (con != null)
>>         con.close();
>>     } catch (SQLException ex) {
>>             out.println("\nERROR:------ SQLException -----\n");
>>             out.println("Message: " + ex.getMessage());
>>             out.println("SQLState: " + ex.getSQLState());
>>             out.println("ErrorCode :" + ex.getErrorCode());
>>         }
>>             }
>>
> 
> The result of con.close() is that the connection is returned to the 
> connection pool.
> This is the purpose of connection pooling: reuse of a connection to 
> increase performance.
> However at program shutdown something needs to close the "real" 
> connection in the connection pool.
> 
> My question is:
> - is closing of connections in the pool handled automatically by Jakarta 
> Commons DBCP ?
> - or do I have do something manaully to close the connections in the pool ?
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


RE: DBCP question: what is closing the actual database connection ?

Posted by Michael Holly <MH...@talisentech.com>.
John

I have created a "Context Listener" class that catalogs the connections
configured in the connection pool when the application starts up and
then listens for when the application or the tomcat server is shut down.
This class tests to see if the pool has any live connections and closes
them before allowing the application to close. Notice I have two
different pools. 

Here is the class listing


import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;

import org.apache.commons.dbcp.BasicDataSource;
import javax.servlet.*;
import org.apache.log4j.Logger;
import java.util.ResourceBundle;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
import java.util.*;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.PropertyConfigurator;

public final class ContextListener
implements ServletContextListener
{

   //get a logger
   Logger log = Logger.getLogger(ContextListener.class);

   private InitialContext initialContext = null;
   private Context namingContext = null;
   private ServletContext context = null;

   public void contextInitialized (ServletContextEvent
servletContextEvent)
   {

      context = servletContextEvent.getServletContext ();
      try
      {

         System.out.println(" \n\n Intializing Context");
         log.info("Initializing logging");
         // configure the Log4j system
         String file = new String( "/WEB-INF/classes/log4j.properties"
);
         URL url = context.getResource(file);
         PropertyConfigurator.configure( url );
         System.out.println("Log4j Properties @ " + url.toString() );


         log.info("Cataloging Context Resources");
         initialContext = new InitialContext();

         namingContext = (Context)
initialContext.lookup("java:comp/env");

         DataSource ds2 =
(DataSource)namingContext.lookup("jdbc/oracle_app");
         DataSource ds1 =
(DataSource)namingContext.lookup("jdbc/mysql_app");

         context.setAttribute("dataSource1", ds1);
         context.setAttribute("dataSource2", ds2);

         log.info("mysql_ftpApp connection pool cataloged");
         log.info("oracle_talisen connection pool cataloged");

      }
      catch (NamingException ne)
      {
         log.error("Couldn't create context attribute: " + ne.getMessage
());
         ne.printStackTrace();
      }
      catch (Exception e)
      {
         log.error("Couldn't create context attribute: " + e.getMessage
());
         e.printStackTrace();
      }
   }

   public void contextDestroyed (ServletContextEvent
servletContextEvent)
   {

      DataSource ds1 = ((DataSource)
context.getAttribute("dataSource1"));
      DataSource ds2 = ((DataSource)
context.getAttribute("dataSource2"));

      try
      {

         log.info("Cleaning up Context Resources");

         if (ds1 instanceof org.apache.commons.dbcp.BasicDataSource) {
            log.info("Found mysql_app connection pool " +
ds1.toString());
            int active = ((org.apache.commons.dbcp.BasicDataSource)
ds1).getNumActive();
            log.info( active + " connection(s)");
            int idle = ((org.apache.commons.dbcp.BasicDataSource)
ds1).getNumIdle();
            log.info( idle + " connection(s)");

            if ((active + idle) > 0 ) {
               ((org.apache.commons.dbcp.BasicDataSource) ds1).close();
               log.info("Data source closed ");
            }

            ds1 = null;
            log.info("Releasing context var");
         }
         log.info("Removed mysql_App connection ");

         if (ds2 instanceof org.apache.commons.dbcp.BasicDataSource) {
            log.info("Found oracle_app connection pool " +
ds2.toString() );
            int active = ((org.apache.commons.dbcp.BasicDataSource)
ds2).getNumActive();
            log.info( active + " connection(s)");
            int idle = ((org.apache.commons.dbcp.BasicDataSource)
ds2).getNumIdle();
            log.info( idle + " connection(s)");

            if ((active + idle) > 0 ) {
               ((org.apache.commons.dbcp.BasicDataSource) ds2).close();
               log.info("Data source closed ");
            }
            ds2 = null;
            log.info("Releasing context var");
         }
         log.info("Removed oracle_app connection");


         context.removeAttribute ("dataSource1");
         context.removeAttribute ("dataSource2");
      }
      catch (Exception e)
      {
         log.error("Error destroying Context: " + e.getMessage ());
         e.printStackTrace();
      }
      finally
      {
 
System.out.println("####################################################
###############################################");
 
System.out.println("####################################################
###############################################");
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("");
      }
   }
}


Here is the entry into the web.xml

<!-- CONTEXT LISTENER -->
    <listener>
 
<listener-class>net.pulse.ftpApp.ContextListener</listener-class>
    </listener>



Hope this helps


Michael




-----Original Message-----
From: John Zoetebier [mailto:john.zoetebier@transparent.co.nz] 
Sent: Saturday, November 08, 2003 11:32 PM
To: Jakarta Commons Users List
Subject: Re: DBCP question: what is closing the actual database
connection ?


On Sun, 9 Nov 2003 03:56:15 +0200, Schalk <sc...@volume4.co.za> wrote:

> John
>
> In basic situation you will have:
> Class.forName(DRIVER);
> con = DriverManager.getConnection(URL,USER,PASS);
>
> At the end you will have:
> finally {
>     try {	
>     if (con != null)
>         con.close();
> 	} catch (SQLException ex) {
>             out.println("\nERROR:------ SQLException -----\n");
>             out.println("Message: " + ex.getMessage());
>             out.println("SQLState: " + ex.getSQLState());
>             out.println("ErrorCode :" + ex.getErrorCode());
>         }
>             }
>

The result of con.close() is that the connection is returned to the 
connection pool.
This is the purpose of connection pooling: reuse of a connection to 
increase performance.
However at program shutdown something needs to close the "real"
connection 
in the connection pool.

My question is:
- is closing of connections in the pool handled automatically by Jakarta

Commons DBCP ?
- or do I have do something manaully to close the connections in the
pool ?

-- 
John Zoetebier
Web site: http://www.transparent.co.nz



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: DBCP question: what is closing the actual database connection ?

Posted by John Zoetebier <jo...@transparent.co.nz>.
On Sun, 9 Nov 2003 03:56:15 +0200, Schalk <sc...@volume4.co.za> wrote:

> John
>
> In basic situation you will have:
> Class.forName(DRIVER);
> con = DriverManager.getConnection(URL,USER,PASS);
>
> At the end you will have:
> finally {
>     try {	
>     if (con != null)
>         con.close();
> 	} catch (SQLException ex) {
>             out.println("\nERROR:------ SQLException -----\n");
>             out.println("Message: " + ex.getMessage());
>             out.println("SQLState: " + ex.getSQLState());
>             out.println("ErrorCode :" + ex.getErrorCode());
>         }
>             }
>

The result of con.close() is that the connection is returned to the 
connection pool.
This is the purpose of connection pooling: reuse of a connection to 
increase performance.
However at program shutdown something needs to close the "real" connection 
in the connection pool.

My question is:
- is closing of connections in the pool handled automatically by Jakarta 
Commons DBCP ?
- or do I have do something manaully to close the connections in the pool ?

-- 
John Zoetebier
Web site: http://www.transparent.co.nz



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


RE: DBCP question: what is closing the actual database connection ?

Posted by Schalk <sc...@volume4.co.za>.
John

In basic situation you will have:
Class.forName(DRIVER);
con = DriverManager.getConnection(URL,USER,PASS);

At the end you will have:
finally {
    try {	
    if (con != null)
        con.close();
	} catch (SQLException ex) {
            out.println("\nERROR:------ SQLException -----\n");
            out.println("Message: " + ex.getMessage());
            out.println("SQLState: " + ex.getSQLState());
            out.println("ErrorCode :" + ex.getErrorCode());
        }
            }

You may want to check the doc for the dbPool you use to get the method for
doing this.

Kind Regards
Schalk Neethling
Web Developer.Designer.Programmer.CEO
Volume4.Development.Multimedia.Branding
emotionalize.conceptualize.visualize.realize
Tel: +27125468436
Fax: +27125468436
email:schalk@volume4.co.za
web: www.volume4.co.za
 
This message contains information that is considered to be sensitive or
confidential and may not be forwarded or diclosed to any other party without
the permission of the sender. If you received this message in error, please
notify me immediately so that I can correct and delete the original email.
Thank you. 

:: -----Original Message-----
:: From: John Zoetebier [mailto:john.zoetebier@transparent.co.nz]
:: Sent: Sunday, November 09, 2003 3:41 AM
:: To: commons-user@jakarta.apache.org
:: Subject: DBCP question: what is closing the actual database connection ?
:: 
:: I have DBCP example code ManualPoolingDriverExample working just fine.
:: To test if the connection is indeed pooled I made a loop which opens and
:: closes the connection.
:: The first connection takes about 2500 ms.
:: Next connections take 1 ms.
:: This looks pretty good to me.
:: 
:: My worry though is what happens if I shutdown the application.
:: What is actually closing down the real database connection stored in the
:: DBCP pool ?
:: Do I have to call a method to indicate that the program is shuttinf down
?
:: 
:: --
:: John Zoetebier
:: Web site: http://www.transparent.co.nz
:: 
:: ---------------------------------------------------------------------
:: To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
:: For additional commands, e-mail: commons-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org