Can I have some simple code for my own connection pooling class?

Christopher Schultz

public class ConnectionPool
{
   private Connection _connection;

   public ConnectionPool(Connection connection)
   {
      _connection = connection;
   }

   public synchronized Connection getConnection()
   {
      while(null == _connection)
      {
         try { wait(); }
         catch (InterruptedException ie) {}
      }
      return(_connection);
   }

   public synchronized void returnConnection(Connection connection)
   {
      if(null != _connection)
         throw new IllegalStateException();

      _connection = connection;
      notify();
   }
}

This will get you by in a pinch. Code to this interface and then expand the class to include more connections, probably in a "real" data structure like a queue.

If you change your implementation but not your interface (except maybe your constructor), you should not have to change any of your servlet code.

-chris
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact