Alex Rivera | Logout

Implementing Connection Pooling : Java

Asked 2011-07-06T10:59:56.810
20

In one of the interviews that I faced,I was asked to implement connection pooling. So approach was this:

  1. Create a List or HashMap
  2. Create predefined number of connections
  3. Add them to the collection.
  4. Now when the ConnectionImpl getConnection() method of ConnectionPoolingImpl class is invoked return a connection reference.

Now when someone returns the connection (releaseConnection(ConnectionImpl O)) how can I ensure that when the same application again tries to reuse the connection object, my implementation throws an exception?

The same connection object might have been returned to a new application and that should be able to use it.

My point of view would be to maintain a flag variable in another array kind of structure for each Connectionimpl object and set that variable to a valid value. When user returns the connection object I would make that some invalid value. For every operation in my ConnectionImpl, I will have to verify if the user had a valid flag.

What would you say to that approach?

Edit
Report

1 Answer

27

I would not return the "real" connection object from the pool, but a wrapper which gives the pool control of connection life cycle, instead of the client.

Assume you have a really simple connection, which you can read int values from:

interface Connection {
    int read(); // reads an int from the connection
    void close(); // closes the connection
}

An implementation reading from a stream could look like this (ignoring exceptions, EOF handling, etc):

class StreamConnection implements Connection {
    private final InputStream input;
    int read(){ return input.read(); }
    void close(){ input.close(); }
}

Furthermore, let's assume you have a pool for StreamConnections that looks like this (again, ignoring exceptions, concurrency, etc):

class StreamConnectionPool {
    List<StreamConnection> freeConnections = openSomeConnectionsSomehow();
    StreamConnection borrowConnection(){ 
        if (freeConnections.isEmpty()) throw new IllegalStateException("No free connections");
        return freeConnections.remove(0); 
    }
    void returnConnection(StreamConnection conn){
        freeConnections.add(conn);
    }
}

The basic idea here is OK, but we can't be sure the connections are returned, and we can't be sure they aren't closed and then returned, or that you don't return a connection which came from another source altogether.

The solution is (of course) another layer of indirection: Make a pool which returns a wrapper Connection which, instead of closing the underlying connection when close() is called, returns it to the pool:

class ConnectionPool {

    private final StreamConnectionPool streamPool = ...;

    Connection getConnection() {
        final StreamConnection realConnection = streamPool.borrowConnection();
        return new Connection(){
           
answered 2011-07-06T11:54:12.870

Your Answer