Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Improve the robustness and performance of your ObjectPool

Go beyond increased app speed and reduced memory requirements: Optimize and stabilize our object pooling utility

  • Print
  • Feedback
Object pooling is a simple and elegant way to conserve memory and enhance speed. By sharing and reusing objects, processes (threads) aren't bogged down by the instantiation and loading time of new objects, or the overhead of excessive garbage collection.

In our first article on object pooling, "Build your own ObjectPool in Java to boost app speed," I implemented a pool of database connections. In this article, I'll expand on that example, adding exception propagation and a clean-up thread.

Exception propagation

To make the object pool as transparent as possible, the borrow and/or get methods should throw the same exception(s) as the constructor of the desired object. Propagation of these exceptions is a simple matter of casting.

Starting from the bottom, the source of any exceptions will be found in the abstraction of the ObjectPool's create() method. Therefore, we simply add the throws Exception qualifier to its declaration.

abstract Object create() throws Exception;


In the JDBCConnectionPool abstraction of the create() method, a single call to DriverManger.getConnection() is made. This can throw an SQLException. Rather than catching the exception and throwing it away, as we did in the first article, we'll propagate it.

Object create() throws SQLException
{  
   return( DriverManager.getConnection( dsn, usr, pwd ) );
}


ObjectPool calls the create() method from checkOut(). Since create() now throws an exception, the checkOut() method must deal with it or pass it on. We'll do the latter by simply adding throws Exception to the declaration of checkOut().

synchronized Object checkOut() throws Exception


Now that the exception has reached the borrowConnection() method of JDBCConnectionPool, it's cast back to an SQLException and passed on to the process that requested the database connection.

public Connection borrowConnection() throws SQLException
{ 
   try
   {
      return( ( Connection ) super.checkOut() );
   }
   catch( Exception e )
   {
      throw( (SQLException) e );
   } 
}


The following blocks of code are now practically interchangeable, because they return the same object and throw the same exception:

Class.forName( ... ).newInstance();
Connection c = DriverManager.getConnection( ... );
JDBCConnectionPool cp = new JDBCConnectionPool( ... );
Connection c = cp.borrowConnection( ... );


The clean-up thread

As stated in the previous article, there is a condition in which the pool can accumulate a lot of objects that never get cleaned up. On top of that, it's rather inefficient to have the expiration logic in the checkOut() method. So we'll kill two birds with one stone (or thread, actually). We start by moving the expiration logic into a separate method called cleanUp(). Then we write a simple little thread that calls this method periodically.

Our thread needs only a pointer back to the object pool and a specified sleep time (passed in via the constructor).

class CleanUpThread extends Thread
{
   private ObjectPool pool;
   private long sleepTime;
   CleanUpThread( ObjectPool pool, long sleepTime )
   {
      this.pool = pool;
      this.sleepTime = sleepTime;
   }
   public void run()
   {
      while( true )
      {
         try
         {
            sleep( sleepTime );
         }
         catch( InterruptedException e )
         {
            // ignore it
         }         
         pool.cleanUp();
      }
   }
}


The thread is instantiated and started in ObjectPool's constructor.

  • Print
  • Feedback

Resources
  • The first JavaWorld article on object pooling in this two-part series, Build your own ObjectPool in Java to boost app speed http://www.javaworld.com/javaworld/jw-06-1998/jw-06-object-pool.html
  • JDBC information at Sun http://java.sun.com/products/jdk/1.1/docs/guide/jdbc/index.html
  • Information about threads and reference objects at Sun http://java.sun.com/products/jdk/1.2/docs/guide/refobs/index.html
  • Garbage collection information from Sun's online Java tutorial http://java.sun.com/docs/books/tutorial/java/javaOO/garbagecollection.html
  • Download the complete source, which includes the core object pooling class and an example implementation using database connections http://www.javaworld.com/jw-08-1998/objectpool/objectpool.zip