Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
A.18 JDBC and Database Connection Pooling
555
background thread to allocate a new connection. Then, wait for
the first available connection, whether or not it is the newly allo
cated one.
if ((totalConnections() < maxConnections) &&
!connectionPending) { // Pending = connecting in bg
makeBackgroundConnection();
try {
wait(); // Give up lock and suspend self.
} catch(InterruptedException ie) {}
return(getConnection()); // Try again.
4. Wait for a connection to become available.
This situation occurs when there is no idle connection and
you've reached the limit on the number of connections. This
waiting should be accomplished without continual polling. It is
best to use the
wait
method, which gives up the thread syn
chronization lock and suspends the thread until
notify
or
notifyAll
is called.
try {
wait();
} catch(InterruptedException ie) {}
return(getConnection());
5. Close connections when required.
Note that connections are closed when they are garbage col
lected, so you don't always have to close them explicitly. But,
you sometimes want more explicit control over the process.
public synchronized void closeAllConnections() {
// The closeConnections method loops down Vector, calling
// close and ignoring any exceptions thrown.
closeConnections(availableConnections);
availableConnections = new Vector();
closeConnections(busyConnections);
busyConnections = new Vector();
}
Connection pool timing results
Condition
Average Time
Slow modem connection to database, 10
11 seconds
initial connections, 50 max connections
(
ConnectionPoolServlet
)
Slow modem connection to database,
22 seconds
recycling a single connection
(
ConnectionPoolServlet2
)
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved