| By Andy Pardue | Article Rating: |
|
| December 24, 2006 10:00 PM EST | Reads: |
23,672 |
Does this sound familiar? You have a domain object, perhaps for reporting purposes, that's built from a ton of JDBC queries and it takes too long to load. Nothing else happens until this object is built, so it's become a bottleneck. Even worse, each of the queries is actually well tuned, so there isn't much to gain from modifying the queries themselves - there are just too many of them. You don't want to change (or can't change) your data model, so what can be done to alleviate this problem short of a major redesign? There are several options like caching, lazy loading, resource pooling. Another worthy option would be to implement a variation of the concurrent query pattern.
Concurrent queries are fairly simple to implement and even simpler to describe. Rather than serializing a set of queries, one after the other, waiting for one to complete before the next begins, one would actually use threads to run sets of independent queries simultaneously. Now, using threading for database I/O might sound daunting or ill-advised, but the Java threads package is one of the best, if not the best, I've had the pleasure of working with. Plus, the new concurrency utilities supplied with Java 5 make using threading for database I/O much more feasible. The UML-ish diagram in Figure 1 attempts to provide a pictorial representation of what I hope to explain in the following paragraphs.
Suppose a domain object is built from 200 queries that can each run from execution to processing results in 100 milliseconds on average. Now 100ms isn't too bad for a query, but stacked end to end, the result would be up to 20 seconds to build an object. Ouch. Now, suppose you could run up to five of these queries concurrently at any one time. In an example that will be described later, I was able to take a similar scenario and increase performance from 20+ seconds to build an object to 5+ seconds. First, though, I'll describe a simpler problem scenario and then implement a solution using concurrent queries, making use of JDBC, connection pooling, and Java 5 thread pools in an effort to demonstrate the type of performance improvements this pattern might render. Later on, I'll cover another, more complicated implementation of an object that's built from several actual database queries. Note: Full source code for these sample implementations is available on sourceforge.net.
A Serialized Baseline Sample Application
First, let's look at the usual case for building objects from database queries. For the following example, a user-defined sleep function was created in a Postgres database (using Postgres magic that I found on the Internet) that could be called as select sleep(N) where N is the number of seconds to sleep. After sleeping for the seconds indicated in the argument the number of seconds slept is returned back as a result. We'll do this with a class called SleepyObject in Listing 1.
This is a fairly standard JDBC query class that selects the sleep(N) function for the number of seconds desired and processes the ResultSet, which simply contains an integer indicating the number of seconds requested to sleep. So, if you "select sleep(1)," the result of that query will be 1. Effectively, the sleep function mimics a query that takes N seconds to complete. In the serialized example (Example1.java), an array of five SleepyObjects is created. Upon creation, each SleepyObject selects the sleep(N) function via JDBC and processes the result. This example creates an array of SleepyObjects then iterates over that array, printing out the return value from the call to the sleep function. Then, the number of seconds it took to execute the entire exercise is printed. Since this sample creates JDBC objects in the usual way, the second SleepyObject isn't created until the first object is fully created (e.g., finished with its sleep(N) query), and so on. So, in this example, since each SleepyObject sleeps 2, 1, 2, 2, and 1 seconds, respectively, the entire application must take at least eight seconds plus overhead to run, as indicated in the output below. This is Example1.java - a serialized example.
package net.sourceforge.concurrentQuery.article.serialized;
import java.sql.SQLException;
public class Example1 {
public Example1() {}
public static void main(String[] args) throws SQLException {
long start = System.currentTimeMillis();
SleepyObject[] sleepyObjects = { new SleepyObject(2),
new SleepyObject(1),
new SleepyObject(2),
new SleepyObject(2),
new SleepyObject(1)
};
int i = 1;
for (SleepyObject sleepyObject : sleepyObjects) {
System.out.println("SleepyObject " + i++ + " returned "
+ sleepyObject.getValue());
}
long end = System.currentTimeMillis();
System.out.println("took: " + new Double(end - start) / 1000 + " seconds");
}
}
The following is output from Example1.java.
run-example1:
[java] query is: select sleep(2)
[java] query is: select sleep(1)
[java] query is: select sleep(2)
[java] query is: select sleep(2)
[java] query is: select sleep(1)
[java] SleepyObject 1 returned 2
[java] SleepyObject 2 returned 1
[java] SleepyObject 3 returned 2
[java] SleepyObject 4 returned 2
[java] SleepyObject 5 returned 1
[java] took: 8.54 seconds
An Implementation Using Concurrent Queries
Next, we'll build on the same example, but this time we'll use a class called ConcurrentSleepyObject to replace SleepyObject. The ConcurrentSleepyObject will use a singleton implementation of the concurrent query pattern to invoke queries and reap the results as seen in Example2.java below.
package net.sourceforge.concurrentQuery.article.concurrent;
import java.sql.SQLException;
public class Example2 {
public Example2() {}
public static void main(String[] args) throws SQLException {
long start = System.currentTimeMillis();
ConcurrentSleepyObject[] concurrentSleepyObjects = {
new ConcurrentSleepyObject(2),
new ConcurrentSleepyObject(1),
new ConcurrentSleepyObject(2),
new ConcurrentSleepyObject(2),
new ConcurrentSleepyObject(1)
};
int i = 1;
for (ConcurrentSleepyObject concurrentSleepyObject :
concurrentSleepyObjects) {
System.out.println("ConcurrentSleepyObject " + i++
+ " returned " + concurrentSleepyObject.getValue());
}
long end = System.currentTimeMillis();
System.out.println("took: " + new Double(end - start) / 1000 + " seconds");
}
}
Both Example1 and Example2 build an array containing five objects, each invokes a database sleep for the same amount of time. However, in this second example, by using an implementation of the concurrent query pattern, the same JDBC calls can be executed in less than half the time (2.86 seconds versus 8.54). This is because we have five queries running at once rather than one at a time. In this example, each of the five queries is run and resolved within its own thread. So, rather than the total execution time being the sum of all queries plus overhead, as in the first example, the total execution time is roughly the amount of time it takes for the longest query to run plus overhead. Here is the output from Example2.java shown below.
run-example2:
[java] query is: select sleep(2)
[java] query is: select sleep(1)
[java] query is: select sleep(2)
[java] query is: select sleep(2)
[java] query is: select sleep(1)
[java] ConcurrentSleepyObject 1 returned 2
[java] ConcurrentSleepyObject 2 returned 1
[java] ConcurrentSleepyObject 3 returned 2
[java] ConcurrentSleepyObject 4 returned 2
[java] ConcurrentSleepyObject 5 returned 1
[java] took: 2.86 seconds
To accomplish this, a class called ConcurrentQueryThreadImpl.java was created as a singleton class that encapsulates:
- The number of active queries that can run at once (five for purposes of this example).
- A ConcurrentHashMap to hold a list of running query threads and a reference to the domain object interface to be used to reap the results of the query. ConcurrentHashMap is a thread-safe HashMap available in the java.util.concurrent package.
- A second ConcurrentHashMap to hold a list of queries and domain object interfaces of queries that couldn't be immediately submitted because the maximum number of threads (five) was already running.
- The needed JDBC code to execute the queries.
The interface CanResolveAConcurrentQuery, referenced in the ConcurrentQueryThreadImpl class, is used in the ConcurrentHashMaps and simply defines two methods that must be implemented by a participant in a concurrent query (e.g., ConcurrentSleepyObject), one to process the SQL results and another (isReaped())method that the ConcurrentQuery implementation can use to indicate to the object that it has processed its SQL results and is ready to go. Below is CanResolveAConcurrentQuery.java.
package net.sourceforge.concurrentQuery.article.concurrent;
import java.sql.ResultSet;
import java.sql.SQLException;
public interface CanResolveAConcurrentQuery {
boolean processResultSet(ResultSet rs) throws SQLException;
void setReaped(boolean isReaped);
}
By implementing this interface, the ConcurrentSleepyObject can participate in concurrent queries. Notice that in the getValue() method the query object needs to make sure that it has been "reaped" (e.g., it either processed its results or threw an SQLException) and if not, the object must call the waitForAllQueriesToComplete() method of the ConcurrentQueryThreadImpl singleton, which submits and processes all outstanding queries. See getValue() method of ConcurrentSleepyObject shown in the following.
public class ConcurrentSleepyObject implements CanResolveA-ConcurrentQuery {
...
public int getValue() throws SQLException {
if (!reaped) {
ConcurrentQueryThreadImpl.getInstance().waitForQueriesToComplete();
}
return value;
}
...
This is done so that the object can be sure that its results have been processed before it can be used. The waitForAllQueriesToComplete() method won't return until all running and queued queries are finished. This way, our object can be sure that its results have been processed before continuing. A better option, though, would be to assign a token, or cookie, to each participant in a concurrent query that can be used by the object to ensure that results are ready. This way, if the results aren't ready yet, the object won't have to wait for all the other queries to finish, but could be notified when its query has completed, perhaps moving it to the front of the queue, if necessary. To keep things simple, I opted for the main -strain-and-brute-force approach of waiting for all queries to finish. The complete source for the ConcurrentSleepyObject class is in Listing 3.
Details of the ConcurrentQuery Implementation
As mentioned, this implementation uses two lists to manage queries. The first list is the threads that are currently running SQL queries and can't surpass the configured value for the application. Since each thread corresponds to a JDBC connection, you want to be careful not to set this value too high. I've used five for this example. The second list is the queued queries that haven't been able to run because the running thread list was full when the query was submitted via the runQuery() method. See Listing 4.
Published December 24, 2006 Reads 23,672
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Andy Pardue
Andy Pardue is a senior software developer who has specialized in the medical software industry for over 15 years, 11 years as a telecommuter from his home office in Mesquite, Texas. He can be reached at: andypardue@gmail.com.
![]() |
JDJ News Desk 12/19/06 01:30:06 PM EST | |||
Does this sound familiar? You have a domain object, perhaps for reporting purposes, that's built from a ton of JDBC queries and it takes too long to load. Nothing else happens until this object is built, so it's become a bottleneck. Even worse, each of the queries is actually well tuned, so there isn't much to gain from modifying the queries themselves - there are just too many of them. You don't want to change (or can't change) your data model, so what can be done to alleviate this problem short of a major redesign? There are several options like caching, lazy loading, resource pooling. Another worthy option would be to implement a variation of the concurrent query pattern. |
||||
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Journal Opens "Readers' Choice Awards" Nominations
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Industry Experts Discuss the State of Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- It's the Java vs. C++ Shootout Revisited!
- The End of IT 1.0 As We Know It Has Begun
- An Introduction to Abbot
- Java Kicks Ruby on Rails in the Butt
- Interviewing Java Developers With Tears in My Eyes
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- 1st Annual Government IT Expo: Call for Papers Deadline July 15
- How to Diagnose Java Resource Starvation
- REA Is Where RIA Becomes the Norm
- Kindle 2 vs Nook
- Anatomy of a Java Finalizer
- Why IBM’s Server Chief Got Busted
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?




























