FileDocCategorySizeDatePackage
ConnectionPool.javaAPI DocExample5706Thu Jun 28 16:14:16 BST 2001com.ora.jsp.sql

ConnectionPool

public class ConnectionPool extends Object
This class implements a connection pool. It's the same as the ConnectionPool class described in Java Servlet Programming (O'Reilly), copied with permission from Jason Hunter. It's used by the DataSourceWrapper class to provide a JDBC 2.0 DataSource interface to the pool.
author
Jason Hunter,
version
1.0

Fields Summary
private Hashtable
connections
private int
increment
private String
dbURL
private String
user
private String
password
Constructors Summary
public ConnectionPool(String dbURL, String user, String password, String driverClassName, int initialConnections, int increment)


        // Load the specified driver class                               
        Class.forName(driverClassName);                                  
                                                                     
        this.dbURL = dbURL;                                              
        this.user = user;                                                
        this.password = password;                                        
        this.increment = increment;                                      
                                                                     
        connections = new Hashtable();                                   
                                                                     
        // Put our pool of Connections in the Hashtable                  
        // The FALSE value indicates they're unused                      
        for(int i = 0; i < initialConnections; i++) {                    
            connections.put(DriverManager.getConnection(dbURL, user, password),
                Boolean.FALSE);                                
        }                                                                
    
Methods Summary
public java.sql.ConnectiongetConnection()

            
        Connection con = null;                                           
                                                                     
        Enumeration cons = connections.keys();                           
                                                                     
        synchronized (connections) {                                    
            while(cons.hasMoreElements()) {                                
                con = (Connection)cons.nextElement();                        
                                                                     
                Boolean b = (Boolean)connections.get(con);                   
                if (b == Boolean.FALSE) {                                    
                    // So we found an unused connection.                       
                    // Test its integrity with a quick setAutoCommit(true) call.
                    // For production use, more testing should be performed,   
                    // such as executing a simple query.                       
                    try {                                                      
                        con.setAutoCommit(true);                                 
                    }                                                          
                    catch(SQLException e) {                                    
                        // Problem with the connection, replace it.              
                        connections.remove(con);                                 
                        con = DriverManager.getConnection(dbURL, user, password);
                    }                                                          
                    // Update the Hashtable to show this one's taken           
                    connections.put(con, Boolean.TRUE);
                    
                    // Return the connection
                    return con;
                }                                                            
            }                                                              
        }                                                                
                                                                     
        // If we get here, there were no free connections.               
        // We've got to make more.                                       
        for(int i = 0; i < increment; i++) {                             
            connections.put(DriverManager.getConnection(dbURL, user, password),
                Boolean.FALSE);                                
        }                                                                
                                                                     
        // Recurse to get one of the new connections.                    
        return getConnection();                                          
    
public voidreturnConnection(java.sql.Connection returned)

                
        if (connections.containsKey(returned)) {                         
            connections.put(returned, Boolean.FALSE);                      
        }