Java Reference
In-Depth Information
The
releaseStatement
method first obtains a
mutex
. This is the same
mutex
used by the
obtainStatement
function. This ensures that no two threads are access-
ing the
PreparedStatement
cache at the same time.
try {
try {
this.mutex.acquire();
} catch (InterruptedException e) {
return;
}
The
PreparedStatement
is added to the cache.
this.statementCache.add(stmt);
Finally, the
mutex
is released.
} finally {
this.mutex.release();
}
Once the
releaseStatement
method is complete, the
PreparedStatement
is back on the cache awaiting the next thread that needs it.
Implementing an SQL Based Workload Manager
The SQL based workload manager is implemented in the
SQLWorkloadManager
class. In this section, I will show you some of the internals of how this class works. This
class makes extensive use of SQL. Teaching extensive SQL is beyond the scope of this topic;
however, many of the methods and functions of the
SQLWorkloadManager
are simply
wrappers for SQL statements. All of the SQL statements used by the spider are relatively
simple, if you know SQL basics. This section will focus on explaining how the class works and
not what the actual SQL does.
The SQL queries used by this class should work with nearly any SQL database, once the
database is set up. For more information about how to set up the database, see Appendix F.
The
SQLWorkloadManager
class is shown in Listing 15.2.
Listing 15.2: SQL Workload Management (SQLWorkloadManager.java)
package com.heatonresearch.httprecipes.spider.workload.sql;
import java.net.*;
import java.sql.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;
import com.heatonresearch.httprecipes.spider.*;