commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: [DBCP] weird behavior?
Date Tue, 07 Jan 2003 20:28:22 GMT


On Tue, 7 Jan 2003, xserty@libero.it wrote:

> Date: Tue,  7 Jan 2003 19:07:51 +0100
> From: "xserty@libero.it" <xserty@libero.it>
> Reply-To: Jakarta Commons Users List <commons-user@jakarta.apache.org>
> To: commons-user <commons-user@jakarta.apache.org>
> Subject: [DBCP] weird behavior?
>
> Hi folks...
> I'm testing my system and almost everything seems to work fine.
>
> First of all, I'd appreciate very much if someone could tell me the difference
> between maxActive and maxIdle parameters in server.xml, since I still can't
> understand.

maxActive is the maximum number of connections that the pool will create
and allow to be used (via a call to dataSource.getConnection()) at the
same time.  Therefore, this is also the limit on how many simultaneous
transactions your app will handle.

maxIdle does not come into play until after the borrowed connections are
returned to the pool, and it defines the maximum number of available
connections to allow in the pool.

Consider a configuration where maxActive=5 and maxIdle=2.  You're app gets
a surge of activity and wants to do four simultaneous requests, so four
connections get created and handed out.  At this moment there are no idle
connections in the pool.

Now, the four transactions complete but no new ones come in.  The first
two returned connections will be returned to the pool -- the other two
will be closed (reducing the load on your database) and thrown away.  Of
course, they might get recreated later, but in the mean time you don't
have any more than maxIdle available connections waiting.

> BTW, if I set them both to 0 (zero), and I try to get a connection, I get a:
> java.sql.SQLException: DBCP could not obtain an idle db connection, pool exhausted.
> Is that right?

Yes.  Saying maxActive=0 says "I never want the pool to create a
connection".

>
> The other thing that bothers me is the following:
> My settings in server.xml are:
> maxActive=2
> maxIdle=2
> removeAbandoned=true
> removeAbandonedTimeout=10
> maxWait=30000
> all the rest are not relevant for this case.
>
> Say I used all available connections in pool without closing them, and I want to
> get another connection.
> I would expect to get one in maximum 10 seconds (more or less).
> While the I'm waiting to get the new connection (maxWait=30sec), an un-closed
> connection should be released and made available (removeAbandonedTimeout=10) for me.
> But after 30 second I get an Exception, and I didn't get the new connection I
> expected.
> Is that the expected behavior, or is there something wrong?
> Is maxWait used only for correctly closed connection made available?

The settings you describe are pretty likely to cause you lots of problems.

The "removeAbandonedXxx" settings are only there to help you catch
programming errors where you borrow a connection from the pool but forget
to return it (usually because some sort of exception happens).  These
settings tell the pool to go retrieve an allocated connection after 10
seconds -- even if it is still in use!

If your app runs out of connections without the removeAbandonedXxx
settings, then it is either experiencing higher demand than your maxActive
value is set for, or your code is broken and leaks connections.  The best
way to avoid the latter problem is to always program your database
usage like this:

  DataSource ds = ...;
  Connection conn = null;
  try {

    conn = ds.getConnection();
    ... use the connection to do work ...

  } catch (SQLException e) {

    ... deal with the exception ...

  } finally {

    if (conn != null) {
      try {
        conn.close(); // Returns conn to the pool
      } catch (SQLException e) {
        ;
      }
      conn = null;
    }

  }

This way, the conn.close() call will *always* happen, even if some of your
processing that uses the connection cause exceptions.


>
> I'm using:
> - J2SDK 1.4.1
> - Tomcat 4.1.12 LE
> - Oracle
> - Windows 98
>
> Kind regards,
> Stephen
>

Craig



Mime
View raw message