On Sat, 28 Jun 2003, Noel J. Bergman wrote:

> Date: Sat, 28 Jun 2003 14:34:05 -0400
> From: Noel J. Bergman <[EMAIL PROTECTED]>
> Reply-To: Jakarta Commons Developers List <[EMAIL PROTECTED]>
> To: Jakarta Commons Developers List <[EMAIL PROTECTED]>,
>      [EMAIL PROTECTED]
> Subject: RE: DBCP status?
>
> > > - Better support/debugging for forcing connections closed after being
> > >   open for "too long"
>
> > This is exactly what got DBCP into trouble in the past.  I'm -1 on
> > providing any ability in DBCP to close lost connections.  DBCP should
> > provide the ability to *log* when it detects a resource leak but the
> > application is responsible for the health of the pool.
>
> I understand your view, but do you believe that there is no possible
> solution?  If it is just an implementation concern, I'd just as soon see
> what solution someone comes up with.  In your opinion, what are/were the
> problems in handling "abandoned" connections in DBCP?
>

I agree with David (and others who think a pool trying to recover things
is a bad idea).

I do not believe there is any fundamentally sound algorithm that a
connection pool can use to detect when a connection has truly been
"abandoned" and is thereby suitable for recovery.  And, grabbing back
connections that are actually in use is *much* worse than leaking them,
because you immediately break an application that is currenty executing,
in ways that are very unpredictable, hard to reproduce, and basically
impossible to recover from.

It's really sad that people writing database driven software using a
connection pool don't seem to be aware of how trivially simple it is to
make sure that connection leaks do not happen to them, using a
try/catch/finally block to ensure that the release always happens.

  DataSource ds = ... reference to your pool ...;
  Connection conn = null;
  PreparedStatement stmt = null;
  ResultSet rs = null;
  try {

    // Allocate a connection from the pool
    conn = ds.getConnection();

    // Use it to perform some processing -- either directly
    stmt = conn.prepareStatement("SELECT ...");
    rs = stmt.executeQuery();
    while (rs.next()) {
      ... process this row ...
    }
    rs.close();
    rs = null;
    stmt.close();
    stms = null;

    // Or pass the allocated connection on to some other method
    doMyDatabaseTransaction(conn);

    // Keep doing transactions on this connection until you are done

  } finally {

    // Release allocated resources, no matter what exception was thrown
    if (rs != null) {
      try {
        rs.close();
      } catch (SQLException e) {
        ; // Or log it
      }
      rs = null;
    }
    if (stmt != null) {
      try {
        stms.close();
      } catch (SQLException e) {
        ; // Or log it
      }
      stmt = null;
    }
    if (conn != null) {
      try {
        conn.close(); // Returns connection to the pool
      } catch (SQLException e) {
        ; // Or log it
      }
      conn = null;
    }
  }

>       --- Noel

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to