commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig McClanahan <craig...@gmail.com>
Subject Re: about basicdatasource
Date Mon, 29 Nov 2004 05:17:18 GMT
When you are done with a connection that you retrieved from a
DataSource, simply call close() on it -- that is the signal to return
the connection to the pool.  (The pool is wrapping the "real"
connection objects, so the underlying database connection is not
closed.)

One common programming error is to forget to do this close() even when
an exception occurs.  A fairly simple way to deal with that potential
problem is a try/catch/finally block:

  DataSource ds = ...; // Get reference to your data source
  Connection conn = null;
  try {
    conn = ds.getConnection();
    ... use the connection ...
  } catch (SQLException e) {
    ... normal exception handling ...
  } finally {
    if (conn != null) {
        conn.close();
    }
  }

Craig


On Sun, 28 Nov 2004 21:41:12 -0700, haipeng du <haipengdu@gmail.com> wrote:
> I have some basic questions about BasicDataSource.
> I use getConnection() to get a connection to database. After I use
> this connection to excute my sql, do I need to call some methods so
> that I can return this connection for other use? If that is necessary,
> How could I return connection?
> Is that good to use other reference to point to connection: such as
> Connection c = datasourc.getConnection()?
> If that is good when that reference is class member?
> Thanks a lot.
> 
> --
> Haipeng Du
> Software Engineer
> Comphealth,
> Salt Lake City
> 
> ---------------------------------------------------------------------
> 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


Mime
View raw message