Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
18.7 Connection Pooling
503
3. Allocate new connections. 
If a connection is required, there is no idle connection available, 
and the connection limit has not been reached, then start a 
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. The 
natural approach is to use the 
wait
 method, which gives up the 
thread synchronization lock and suspends the thread until 
notify
 or 
notifyAll
 is called. Since 
notifyAll
 could stem 
from several possible sources, threads that wake up still need to 
test to see if they can proceed. In this case, the simplest way to 
accomplish this task is to recursively repeat the process of trying 
to obtain a connection.
try {
wait();
} catch(InterruptedException ie) {}
return(getConnection());
It may be that you don't want to let clients wait and would rather 
throw an exception when no connections are available and the 
connection limit has been reached. In such a case, do the follow 
ing instead:
throw new SQLException("Connection limit reached");
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.
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




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

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