Java Reference
In-Depth Information
// Create the statement
Statement statement = con.createStatement();
// Use the created statement to SELECT the DATA
// FROM the Titles Table.
ResultSet rs = statement.executeQuery(“SELECT * “ +
“FROM Titles”);
// iterate over the results here, if you choose!!!
// Close the ResultSet
rs.close();
}
}
catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
catch (Exception e) {
System.err.println(e.getMessage());
}
finally {
// Release the connection
pool.releaseConnection(con);
}
out.close();
}
When the
doPost()
method is finished with the connection, it must return it to the pool. To do
this, it calls the
ConnectionPools
's
releaseConnection()
method, passing it the
Connection
object:
// Release the connection
pool.releaseConnection(con);
The
releaseConnection()
method searches the pool for the
Connection
object and marks it
as available for use.
The last interaction a servlet has with the
ConnectionPool
is when it shuts down. In its
destroy()
method, the servlet calls the
ConnectionPool.emptyPool()
method. This method
iterates over the entire pool, closing connections until all the
Connection
objects are closed.
Now you have access to pre-opened connections to the database and can service just about any
number of simultaneous requests.