You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Paul Hsu <hs...@verizon.net> on 2005/02/10 19:14:17 UTC

DBCP

Hi,

I have one question about DBCP. I like to know if any one have used BasicDataSource.close(). In my program I set up a BasicDataSource and get connection from MYSQL, I call BasicDataSource.close() right after get connection, I still see the connectioin from MYSQL admin. I just wonder this function is working?


thanks,

Paul

Re: DBCP

Posted by Craig McClanahan <cr...@gmail.com>.
True ... old habits die hard :-)

Craig


On Thu, 10 Feb 2005 20:25:57 +0100, Bernard D'Have <bd...@tiscali.be> wrote:
> I think the first conn.close is unneeded, because the finally block is
> always executed.
> 
> Bernard
> 
> -----Original Message-----
> From: Craig McClanahan [mailto:craigmcc@gmail.com]
> Sent: Thursday, February 10, 2005 7:24 PM
> To: Jakarta Commons Developers List
> Subject: Re: DBCP
> 
> Calling BasicDataSource.close() will only close the connections still in the
> pool -- not the ones that have been checked out.  It is designed to be
> called only when your app is ready to shut down.
> 
> For normal usage, the best approach is something like this:
> 
>     DataSource ds = ... get your data source reference;
>     Connection conn = null;
>     try {
>         conn = ds.getConnection();
>         ... use the connection as needed ...
>         conn.close(); // Returns this connection to the pool
>     } catch (SQLException e) {
>         ... deal with any exception ...
>     } finally {
>         if (conn != null) {
>             try {
>                 conn.close();
>             } catch (SQLException e) {
>                 ...
>             }
>         }
>     }
> 
> That way, you're always returning the connection to the pool, even if an
> exception occurs while you're using it.
> 
> BTW, your MySQL admin will show active connections for all the entries in
> the pool, as well as those that have been checked out and are in use.
> 
> Craig
> 
> On Thu, 10 Feb 2005 10:14:17 -0800, Paul Hsu <hs...@verizon.net> wrote:
> > Hi,
> >
> > I have one question about DBCP. I like to know if any one have used
> > BasicDataSource.close(). In my program I set up a BasicDataSource and
> > get connection from MYSQL, I call BasicDataSource.close() right after
> > get connection, I still see the connectioin from MYSQL admin. I just
> > wonder this function is working?
> >
> > thanks,
> >
> > Paul
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 
>

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


RE: DBCP

Posted by Bernard D'Have <bd...@tiscali.be>.
I think the first conn.close is unneeded, because the finally block is
always executed.

Bernard

-----Original Message-----
From: Craig McClanahan [mailto:craigmcc@gmail.com] 
Sent: Thursday, February 10, 2005 7:24 PM
To: Jakarta Commons Developers List
Subject: Re: DBCP


Calling BasicDataSource.close() will only close the connections still in the
pool -- not the ones that have been checked out.  It is designed to be
called only when your app is ready to shut down.

For normal usage, the best approach is something like this:

    DataSource ds = ... get your data source reference;
    Connection conn = null;
    try {
        conn = ds.getConnection();
        ... use the connection as needed ...
        conn.close(); // Returns this connection to the pool
    } catch (SQLException e) {
        ... deal with any exception ...
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                ...
            }
        }
    }

That way, you're always returning the connection to the pool, even if an
exception occurs while you're using it.

BTW, your MySQL admin will show active connections for all the entries in
the pool, as well as those that have been checked out and are in use.

Craig

On Thu, 10 Feb 2005 10:14:17 -0800, Paul Hsu <hs...@verizon.net> wrote:
> Hi,
> 
> I have one question about DBCP. I like to know if any one have used 
> BasicDataSource.close(). In my program I set up a BasicDataSource and 
> get connection from MYSQL, I call BasicDataSource.close() right after 
> get connection, I still see the connectioin from MYSQL admin. I just 
> wonder this function is working?
> 
> thanks,
> 
> Paul
>

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


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


Re: DBCP

Posted by David Graham <gr...@yahoo.com>.
--- Craig McClanahan <cr...@gmail.com> wrote:

> Calling BasicDataSource.close() will only close the connections still
> in the pool -- not the ones that have been checked out.  It is
> designed to be called only when your app is ready to shut down.
> 
> For normal usage, the best approach is something like this:
> 
>     DataSource ds = ... get your data source reference;
>     Connection conn = null;
>     try {
>         conn = ds.getConnection();
>         ... use the connection as needed ...
>         conn.close(); // Returns this connection to the pool
>     } catch (SQLException e) {
>         ... deal with any exception ...
>     } finally {
>         if (conn != null) {
>             try {
>                 conn.close();
>             } catch (SQLException e) {
>                 ...
>             }
>         }
>     }

It's this kind of drudgery that prompted me to volunteer on DbUtils. 
Check it out if you're tired of JDBC resource cleanup.

http://jakarta.apache.org/commons/dbutils/

David


> 
> That way, you're always returning the connection to the pool, even if
> an exception occurs while you're using it.
> 
> BTW, your MySQL admin will show active connections for all the entries
> in the pool, as well as those that have been checked out and are in
> use.
> 
> Craig
> 
> On Thu, 10 Feb 2005 10:14:17 -0800, Paul Hsu <hs...@verizon.net>
> wrote:
> > Hi,
> > 
> > I have one question about DBCP. I like to know if any one have used
> BasicDataSource.close(). In my program I set up a BasicDataSource and
> get connection from MYSQL, I call BasicDataSource.close() right after
> get connection, I still see the connectioin from MYSQL admin. I just
> wonder this function is working?
> > 
> > thanks,
> > 
> > Paul
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 
> 



		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - Easier than ever with enhanced search. Learn more.
http://info.mail.yahoo.com/mail_250

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


Re: DBCP

Posted by Craig McClanahan <cr...@gmail.com>.
Calling BasicDataSource.close() will only close the connections still
in the pool -- not the ones that have been checked out.  It is
designed to be called only when your app is ready to shut down.

For normal usage, the best approach is something like this:

    DataSource ds = ... get your data source reference;
    Connection conn = null;
    try {
        conn = ds.getConnection();
        ... use the connection as needed ...
        conn.close(); // Returns this connection to the pool
    } catch (SQLException e) {
        ... deal with any exception ...
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                ...
            }
        }
    }

That way, you're always returning the connection to the pool, even if
an exception occurs while you're using it.

BTW, your MySQL admin will show active connections for all the entries
in the pool, as well as those that have been checked out and are in
use.

Craig

On Thu, 10 Feb 2005 10:14:17 -0800, Paul Hsu <hs...@verizon.net> wrote:
> Hi,
> 
> I have one question about DBCP. I like to know if any one have used BasicDataSource.close(). In my program I set up a BasicDataSource and get connection from MYSQL, I call BasicDataSource.close() right after get connection, I still see the connectioin from MYSQL admin. I just wonder this function is working?
> 
> thanks,
> 
> Paul
>

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