Java Reference
In-Depth Information
A JDBC Connection Pool
To remedy the slow servicing of requests encountered in the previous section, you will create a
pool of connections to the database. This will give you access to a collection of already opened
database connections, which will reduce the time it takes to service a request, and you can ser-
vice n number of requests at once.
The following are the requirements for the ConnectionPool object:
• It must hold n number of open connections.
• It must be able to determine when a connection is in use.
•If n+1 connections are requested, it must create a new connection and add it to the pool.
When you close the pool, all connections must be released.
Now that we know what we want, let's look at the result. The source for the ConnectionPool
is in Listing 7.6.
L ISTING 7.6
ConnectionPool.java
package ConnectionPool;
import java.sql.*;
import java.util.*;
public class ConnectionPool {
// JDBC Driver Name
private String driver = null;
// URL of database
private String url = null;
// Initial number of connections.
private int size = 0;
// Username
private String username = null;
// Password
private String password = null;
// Vector of JDBC Connections
private Vector pool = null;
public ConnectionPool() {
}
// Set the value of the JDBC Driver
Search WWH ::




Custom Search