Java Cart .

Java Cart DBConnectionPool java

package cart.dbUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Stack;

//DBConnectionPool
//author - Lawrence Truett - FluffyCat.com
//date - May 27, 2003 - San Diego, CA
public class DBConnectionPool {
    private Stack openConnections = new Stack();
    private ArrayList usedConnections = new ArrayList();
    private int connectionCount = 0;
    private int connectionMax = 10;//max number of connection we can have
    
    public DBConnectionPool() {
        //load database driver
        try {
            Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        } catch(java.lang.ClassNotFoundException cnfException) {
            System.out.println(
                "The following error occured in finding "+
                "the DataBase driver: " + 
                cnfException);
        } catch(java.lang.InstantiationException iException) {
            System.out.println(
                "The following error occured in finding "+
                "the DataBase driver: " + 
                iException);
        } catch(java.lang.IllegalAccessException iaException) {
            System.out.println(
                "The following error occured in finding "+
                "the DataBase driver: " + 
                iaException);
        }
    }
    
    public DBConnection getConnection() {
        DBConnection dbc = null;
        if (!openConnections.empty()) {
            dbc = (DBConnection)openConnections.pop();
            usedConnections.add(dbc);
            return dbc;
        }
        if (connectionCount < connectionMax) {
            dbc = new DBConnection();
            usedConnections.add(dbc);
            return dbc;
        }
        return dbc;
    }
    
    public void returnConnection(DBConnection dbc) {
        usedConnections.remove(dbc);
        openConnections.add(dbc);
    }
}
To download source right-click here and "Save As...".
Comments
Sign in to be the first to comment on Java Cart DBConnectionPool java.