commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rodney Waldhoff <rwaldh...@apache.org>
Subject RE: DBCP not opening multiple connections
Date Mon, 09 Dec 2002 23:53:51 GMT
Think of it this way:

The first time you obtain a connection from DBCP (via a DataSource or the
DriverManager or whatever), it creates a new network connection to the
database, more or less like you normally would.

When you close that connection, it doesn't really close the network
connection, it simply returns it to the "pool" of connections that are
available.  I'll call these the "idle" connections.

The next time you obtain a connection from DBCP, it will check the pool to
see if there are any idle connections available.  If there are, it simply
returns one of them (rather than creating a new one).  If there aren't, it
will create a new one.

In this way, a single thread of execution repeatedly calling
getConnection/Connection.close will think it is getting a new Connection
each time, but it's really the same one.  E.g., assuming the pool is
initially empty

client calls getConnection()
 --> dbcp creates a new Connection (C1) and returns it
     (the "idle" pool is now empty)

client calls C1.close()
 --> dbcp adds C1 to the pool of idle connections
     (the "idle" pool now contains C1)

client calls getConnection()
 --> dbcp returns C1 from the pool again
     (the "idle" pool is now empty)

client calls C1.close()
 ---> dbcp adds C1 to the pool of idle connections again
      (the "idle" pool now contains C1)

and so on.

The only time we'll need to create more than one connection is when more
than one client needs a connection at the same time.  For example:

client A calls getConnection()
 --> dbcp creates a new Connection (C1) and returns it
     (the "idle" pool is now empty)

client B calls getConnection()
 --> since the pool is empty, dbcp creates a new Connection (C2)
     and returns it
     (the "idle" pool is still empty, but now there are two
     active connections)

client A calls C1.close()
 --> dbcp adds C1 to the pool of idle connections
     (the "idle" pool now contains C1)

client B calls C2.close()
 ---> dbcp adds C2 to the pool of idle connections
      (the "idle" pool now contains C1 and C2)

So very roughly speaking, the number of actual database connections
created is determined by the number of Connections opened but not closed
at any one time.  E.g., if you call:

for(int i=0;i<N;i++) {
 conn[i] = getConnection();
}
for(int i=0;i<N;i++) {
 conn[i].close();
}

then N physical connections to the database will be created (or attempted
at least).

If you call:

for(int i=0;i<N;i++) {
 conn[i] = getConnection();
 conn[i].close();
}

then only one Connection will be created.

If you have something like:

class MyRunnable implements Runnable {
  void run() {
    for(;;) {
     Connnection conn = getConnection();
     // do something with conn
     conn.close();
     // do something else
    }
  }
}

for(int i=0;i<N;i++) {
  thread[i] = new Thread(new MyRunnable());
  thread[i].start();
}

then you should have between 1 and N physical connections, depending upon
how many threads are executing the "do something with conn" part at the
same time.

This glosses over a number of details, but that's the gist of it.


Mime
View raw message