FileDocCategorySizeDatePackage
ConnectionPool.javaAPI DocExample4833Thu Apr 05 01:53:30 BST 2001None

ConnectionPool

public class ConnectionPool extends Object

Fields Summary
private Hashtable
connections
private Properties
props
Constructors Summary
public ConnectionPool(Properties props, int initialConnections)


      
                      
    this.props = props;
    initializePool(props, initialConnections);
  
public ConnectionPool(String driverClassName, String dbURL, String user, String password, int initialConnections)

    props = new Properties();
    props.put("connection.driver", driverClassName);
    props.put("connection.url", dbURL);
    props.put("user", user);
    props.put("password", password);
    initializePool(props, initialConnections);
  
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 = getNewConnection();
          }                                                          
          // 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.  Make one more.
      // A more robust connection pool would have a maximum size limit,
      // and would reclaim connections after some timeout period
      con = getNewConnection();
      connections.put(con, Boolean.FALSE);
      return con;
    }                                                                
  
private java.sql.ConnectiongetNewConnection()

    return DriverManager.getConnection(
      props.getProperty("connection.url"), props);
  
private voidinitializePool(java.util.Properties props, int initialConnections)

    // Load the driver
    Class.forName(props.getProperty("connection.driver"));

    // Put our pool of Connections in the Hashtable                  
    // The FALSE value indicates they're unused                      
    for(int i = 0; i < initialConnections; i++) {                    
      Connection con = getNewConnection();
      connections.put(con, Boolean.FALSE);                                
    }                                                                
  
public voidreturnConnection(java.sql.Connection returned)

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