Java Reference
In-Depth Information
Repeatable Statements
Java provides a class, named PreparedStatement , that allows you to compile
SQL into objects that can quickly be executed. Because the SQL workload manager only
executes a little over a dozen unique SQL statements, it makes sense to package each of
these SQL statements as a PreparedStatement . This will allow the SQL statements
to be executed more quickly. However, it is very important that no two threads use the same
PreparedStatement . Because of this, it is necessary to create a class that will keep
the PreparedStatement objects from different threads apart.
Additionally, we need to build in the ability to repeat an SQL statement. The spider is
designed to potentially run for days or weeks when using an SQL workload. The connection
to the database could be broken in that time period; therefore, we need a way to continue re-
executing an SQL statement if it fails. To accomplish both of these design requirements, the
RepeatableStatement class was developed.
Before I show you how the RepeatableStatement class was constructed, we
will briefly examine how it is used. The following code uses a RepeatableStatement
to execute a simple SQL query.
String sql = "SELECT * from table_name where name = ?";
RepeatableStatement statement = new RepeatableStatement(sql);
statement.create(manager);
RepeatableStatement.Result result = statement.
executeQuery("Fred");
You will notice that the SQL contains a question mark (?). This is a parameter. The pa-
rameters you pass on the execute or executeQuery function calls will be substi-
tuted for the question mark. In the above example, the value of “Fred” is passed in for the
question mark.
The RepeatableStatement class is shown in Listing 15.1.
Listing 15.1: Repeatable Statements (RepeatableStatement.java)
package com.heatonresearch.httprecipes.spider.workload.sql;
import java.sql.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;
import com.heatonresearch.httprecipes.spider.workload.*;
Search WWH ::




Custom Search