import java.sql.*; import java.util.*; /** * A very, very simple connection pooling class for providing * clients with a connection to the database. * Clients call getConnection() and it tries to give the client * an existing connection, or creates one if one does not already * exist. */ public class DatabaseHook { /* Large database connection and driver info. */ private static String DEFAULT_USER = "[username omitted]"; private static String DEFAULT_PASS = "[password omitted]"; private static String DEFAULT_URL = "[url omitted]"; private static String JDBC_DRIVER = "org.gjt.mm.mysql.Driver"; private static ArrayList connections = new ArrayList(); private static Integer id = new Integer(1); public static ConnectionWrapper getConnection() { ConnectionWrapper conn = getConnection(DEFAULT_URL, DEFAULT_USER, DEFAULT_PASS); return conn; } public static synchronized ConnectionWrapper getConnection(String url, String user, String pass) { Iterator i = connections.iterator(); ConnectionWrapper c; while (i.hasNext()) { c = (ConnectionWrapper) i.next(); if (c.checkOut()) { return c; } } c = openNewConnection(url, user, pass); c.checkOut(); return c; } private static synchronized ConnectionWrapper openNewConnection(String url, String user, String pass) { Connection con = null; ConnectionWrapper w; try { Class.forName(JDBC_DRIVER); con = DriverManager.getConnection(url, user, pass); w = new ConnectionWrapper(con); connections.add(w); } catch (Exception e) { e.printStackTrace(System.err); w = null; } return w; } }