You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by haipeng du <ha...@gmail.com> on 2004/11/29 05:41:12 UTC

about basicdatasource

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


Re: about basicdatasource

Posted by Craig McClanahan <cr...@gmail.com>.
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 <ha...@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