/** * TCNJ Computer Science Research Group - Clay Project * Fall 2001 * * Filename: JDBCConnectionPool.java * Version: 2001111800 * Last Change Made: 11-18-01 * Last Change Made By: Michael Hulme * CurrentStatus: freshly cooked * * @author locasto2@tcnj.edu * @author hulme2@tcnj.edu */ package edu.tcnj.cs.clay.util; import java.util.*; import java.sql.*; public class JDBCConnectionPool implements Runnable { public static final int DEFAULT_MIN_CONNECTIONS = 5; public static final int DEFAULT_MAX_CONNECTIONS = 20; public static final int DEFAULT_CONNECTION_INCREMENT = 5; private Vector available; private Vector busy; private String driverName; private String connectionURL; private String username; private String password; private int minConn; private int maxConn; private JDBCConnectionPool(){ } public JDBCConnectionPool( String driverName, String url, String username, String password, String minConn, String maxConn ){ this.driverName = driverName; connectionURL = url; this.username = username; this.password = password; try{ this.minConn = Integer.parseInt( minConn ); this.maxConn = Integer.parseInt( maxConn ); this.minConn = Math.abs( this.minConn ); this.maxConn = Math.abs( this.maxConn ); }catch( NumberFormatException nfe ){ minConn = DEFAULT_MIN_CONNECTIONS; minConn = DEFAULT_MIN_CONNECTIONS; } available = new Vector( maxConn ); busy = new Vector( maxConn ); } public synchronized Connection getConnection() throws SQLException { if (!available.isEmpty()) { Connection existingconnection = (Connection)available.lastElement(); int lastindex = available.size() - 1; available.removeElementAt(lastindex); if (existingconnection.isClosed()) { notifyAll(); return(getConnection()); } else { busy.addElement(existingconnection); if (maxconnections%DEFAULT_CONNECTION_INCREMENT==1) addMaxConnections(); return(existingconnection); } } else { if ((totalConnections() < maxconnections) && !connectionpending) { makeBackgroundConnection(); } else if (!waitIfBusy) { throw new SQLException("Connection limit reached"); } try { wait(); } catch (InterruptedException ie) { System.out.println(ie.getMessage()); } return (getConnection()); } } private void addMaxConnections() { maxconnections+=DEFAULT_CONNECTION_INCREMENT; Vector temp = new Vector(maxconnections); for (int i = 0; iDEFAULT_CONNECTION_INCREMENT) { maxconnections-=DEFAULT_CONNECTION_INCREMENT; Vector temp = new Vector(maxconnections); for (int i = 0; i