Java Reference
In-Depth Information
The
initializePool()
method first loads the JDBC driver. It then creates
size
number of
Connection
objects. For each
Connection
object created, it creates a
PooledConnection
object, passing it the
Connection
in the constructor. Finally, it adds the newly create
PooledConnection
object to the pool.
The
PooledConnection
object simply wraps a JDBC
Connection
object in a class that holds
the connection and a flag that determines whether the connection is in use or not. It is listed in
Listing 7.9.
L
ISTING
7.9
PooledConnection.java
package ConnectionPool;
import java.sql.*;
public class PooledConnection {
// Real JDBC Connection
private Connection connection = null;
// boolean flag used to determine if connection is in use
private boolean inuse = false;
// Constructor that takes the passed in JDBC Connection
// and stores it in the connection attribute.
public PooledConnection(Connection value) {
if ( value != null ) {
connection = value;
}
}
// Returns a reference to the JDBC Connection
public Connection getConnection() {
// get the JDBC Connection
return connection;
}
// Set the status of the PooledConnection.
public void setInUse(boolean value) {
inuse = value;
}