Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
516
Chapter 18 JDBC and Database Connection Pooling
Using Singleton Classes to Share Connection 
Pools
Rather than using the 
ServletContext
 to share connection pools, you can
use normal static methods. For example, you could write a 
BookPool
 class
with static 
getPool
 and 
setPool
 methods and have each servlet check
BookPool.getPool
 to see if the value is non null, instantiating a new 
Con 
nectionPool
 if necessary. However, each servlet has to repeat similar code,
and a servlet could accidentally overwrite the shared pool that 
Book 
Pool.getPool
 returns. 
A better approach is to use a singleton class to encapsulate the desired
behavior. A singleton class is simply a class for which only a single instance
can be created, enforced through use of a private constructor. The instance is
retrieved through a static method that checks if there is already an object
allocated, returning it if so and allocating and returning a new one if not. For
example, here is the outline of a singleton 
BookPool
 class. Each servlet that
used it would obtain the connection pool by simply calling 
BookPool.getIn 
stance()
.
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