Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Improve the robustness and performance of your ObjectPool - JavaWorld - August 1998

Improve the robustness and performance of your ObjectPool - JavaWorld - August 1998

Tutorial Details:

Improve the robustness and performance of your ObjectPool
Improve the robustness and performance of your ObjectPool
By: By Thomas E. Davis
Go beyond increased app speed and reduced memory requirements: Optimize and stabilize our object pooling utility
bject 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.
cleaner = new CleanUpThread( this, expirationTime );
cleaner.start();
The new cleanUp() method, which gets called by the thread every expirationTime millisecond, simply releases all the old objects in the pool and asks the garbage collector to reclaim them.
synchronized void cleanUp()
{
Object o;
long now = System.currentTimeMillis();
Enumeration e = unlocked.keys();
while( e.hasMoreElements() )
{
o = e.nextElement();
if( ( now - ( ( Long ) unlocked.get( o ) ).longValue() ) >
expirationTime )
{
unlocked.remove( o );
expire( o );
o = null;
}
}
System.gc();
}
Check out the full source for ObjectPool to see what checkOut() looks like without the expiration logic in it. The full source for JDBCConnectionPool is available as well.
Response to reader comments
Several readers had questions after reading the first object-pool article. I address the most relevant questions below.
How do I know when object pooling will benefit my situation?
If you have many threads running in parallel that create, use, and dispose a common object quite frequently, you should consider implementing a pool for that object -- especially if the instantiation and/or garbage collection of the object is an expensive operation. Database and socket connections are a great example.
What do I do about processes that check out objects and never return them?
This is a double-edged sword. At some point, you have to leave responsibility in the hands of the process that uses the pool; you can't make the pool completely idiot-proof...at least not yet.
If a process checks out an object and simply never checks it back in, there's nothing the pool can do about it. As long as the process has a reference to the object, it could use it at any time; any interference on the part of the pool could be catastrophic.
The next big release of Sun's JDK will include reference objects. These will allow the pool to detect (via some wrapper-class implementation) when a checked-out object has been "freed" by the process so the pool can reclaim the object. As an interim solution, if the pool detects that an object has been checked out for an abnormal amount of time (as defined by you, the programmer), the pool can simply release its reference to the object. Thus, if and when the process releases its reference, the object will be garbage collected.
How can I limit the size of the pool?
I strongly discourage limiting the size of the pool. Besides defeating the whole purpose of the pool, limiting its size will open the doors to a loss in performance and the possibility of some nasty deadlocks. However, if you really must, you can put some size-checking logic in checkOut() to make sure it doesn't create more copies than desired. Then, also within checkOut() , you will have to utilize the wait() and notify() methods of Thread to ensure that a call to the pool does not return until an object is available.
Conclusion
With exception propagation and the clean-up thread, the ObjectPool class becomes more robust and one step closer to being transparent. I hope this tool proves as useful for you as it has been to me. As always, I welcome your comments, criticisms, and design-improvement suggestions.
This page formated for crawlers and browsers that don't support scripts and tables.
Home
EZone


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Improve the robustness and performance of your ObjectPool - JavaWorld - August 1998

View Tutorial:
Improve the robustness and performance of your ObjectPool - JavaWorld - August 1998

Related Tutorials:

Accelerate your RMI programming
Accelerate your RMI programming
 
Pick up performance with generational garbage collection
Pick up performance with generational garbage collection
 
Achieve strong performance with threads, Part 1
Achieve strong performance with threads, Part 1
 
Study guide Achieve strong performance with threads Part 1
Study guide Achieve strong performance with threads Part 1
 
Put Java in the fast lane
Put Java in the fast lane
 
Worth reading
Worth reading
 
JView 2004 - J2EE Performance Management Software - Version 1.4
JView 2004 Enterprise Edition available now! JView 2004 is the leading J2EE Production Performance Monitor and Java Performance Tuning solution, providing precise real-time performance metrics to assist you in ensuring your performance requirements a
 
J2EE application performance optimization
J2EE application performance optimization How to extract maximum performance from your J2EE Web applications In this article, Rahul Kuchhal demonstrates how to identify and resolve bottlenecks in a J2EE application. This article covers all the step
 
FindBugs, Part 1: Improve the quality of your code
FindBugs, Part 1: Improve the quality of your code One of the problems with code quality tools is that they tend to overwhelm developers with problems that aren't really problems -- that is, false positives. When false positives occur, developers learn
 
new Version 0.9.3 of the Quake2 Java port Jake2
With the new Jake2 release 0.9.3 savegames are working. So you can take a break on your mission.
 
Servlet and JSP performance tuning
Servlet and JSP performance tuning This article describes performance-tuning techniques (PTT) for developing high performance and scalable JSP (JavaServer Pages) pages and servlets. That means building applications that are reasonably and consistently fa
 
JPOX Java Persistent Objects JDO
Presenting JPOX With a versatile and high performance implementation, JPOX is on the cutting edge of the available Java Data Objects (JDO) implementations, offering a free and full compliant JDO implementation released under an open source license.
 
Java look and feel icons
These pages contain a collection of toolbar button graphics. The graphics have been designed specifically for use with the Java look and feel. They conform to the Java look and feel Design Guidelines (see the "Designing Button Graphics" section of the "Ap
 
Ubik
Overview Ubik aims to provide a set of distributed computing APIs that complement Java's current "official" offerings - such as EJB and Jini. The main API of the Ubik project is a RMI-like framework that allows to easily and transparently perform method
 
The JDBC RowSet Implementations Tutorial
In "The JDBC RowSet Implementations Tutorial," you will look at how to use the standard JDBC RowSet implementations specified in JSR-114.
 
A well-behaved Jetspeed portlet
This article presents a working example of how to construct a Jetspeed portlet that runs efficiently, adheres to the Model 2 architecture, and, by not interfering with additional portlets, plays well with others. In addition, I demonstrate some simple way
 
Java Resources
There are all Java freebies. Some of these are old, and not under maintenance. Download and use them at your risk. In case of queries, mail subrahmanyam_avb@technologist.com or varalakshmi_a@techie.com.
 
Bioinformatics
Bioinformatics Bioinformatics Introduction Bioinformatics is new hot topic after the Software. In the coming days there will be huge demand of Bioinformatics professionals in all sectors of biotechnology, pharmaceutical, and biomedical sciences.
 
What is Persistence Framework?
What is Persistence Framework? What is Persistence Framework? A persistence framework moves the program data in its most natural form (in memory objects) to and from a permanent data store the database. The persistence framework manages the
 

Free Web Site Hosting Services Below is the listing of the hosting providers providing free web hosting services. These services helps you building your sites even if you have no experience in HTML writing. Zero
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.