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

All aboard for more efficient Web applications

The Train architecture dynamically batches user requests to improve server performance

  • Print
  • Feedback

Imagine a railroad station operating in such a way that each passenger who buys a ticket immediately gets a train dedicated only to him! This modus operandi is absurd in real life, but is widely accepted in the world of Web application servers and data access applications. The conventional paradigm implies that each user request receives its own thread and database connection. Each user request requires an immediate dedicated trip to the database or other network resource. Obviously, there should be a smarter way of handling external traffic than buying extra hardware. Let's explore a simple but overlooked way of increasing your application's productivity.

In this article, we employ a new approach where each interaction with the database or network resource occurs on behalf of multiple users rather than only one, where negative effects of high concurrency like timeouts and deadlocks are greatly reduced, and where heavy traffic performance regression is almost negligible.

To be able use this approach—a paradigm I call Train—we must create an adequate environment for running our application and perform proof-of-concept tests.

Build a sandbox

To illustrate the advantage of the proposed architecture, we are going to build two simple, functionally equivalent servlets. Both deliver the same HTML pages with data retrieved from a sample database. Each servlet represents a different implementation—the conventional paradigm and the new one. To build our servlets, we need a Web application server, a database and a load-test runner. You are free to use your software of choice; my pieces are Tomcat 4, JMeter, and IBM'S DB2 Universal Database. Tomcat 4 and JMeter are open source applications and free. The choice of DB2 is just an attempt to imitate the commercial Web environment as closely as possible.

Populate the database with random content

Assuming the name of your database is "trdata," let's create the necessary schema:

 //schema.sql
connect to trdata;
create table trentry (ID integer not null , NAME char(25), DESCR varchar(128), views integer with default 0, constraint p_trentry  primary key (ID));



The simple Java application PropSamples populates table trentry with 250,000 rows of random content:

 //PropSamples.java
package train;
import java.util.*;
import java.sql.*;
public class PropSamples {

public static String GetString(int size, Random rand) { StringBuffer strBuff = new StringBuffer(); for (int i = 0; i < size; i++) { char b = (char) (rand.nextInt(25) + 65); strBuff.append(b); } return strBuff.toString(); }

public void Process() throws SQLException { String sqlString = "insert into trentry values(?,?,?,?)"; Connection connection = Util.getDBConnection(); PreparedStatement stmt = connection.prepareStatement(sqlString); Random rand = new Random(); for (int i = 1; i <= 250000; i++) { stmt.clearParameters(); stmt.setInt(1, i); stmt.setString(2, GetString(25, rand)); stmt.setString(3, GetString(128, rand)); stmt.setInt(4, 0); stmt.execute(); if (i%1000 == 0) { connection.commit(); System.out.println(i + " rows committed"); } } } public static void main(String[] args) throws SQLException{ PropSamples propSamples = new PropSamples(); propSamples.Process(); } }



This code is nothing to write home about. Class Util (available in the source code, which is downloadable from Resources) contains DB2-related specifics that should change depending on the environment.

  • Print
  • Feedback

Resources