/* Copyright (c) 2004 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ package cc.glsn; import java.util.Iterator; import java.util.LinkedList; /** Manages a group of SQL connections and gives them out to threads who ask*/ public class DBConnMgr { private int MaxConn; private LinkedList Ready; private LinkedList NotOpen; private ConfigFile Config; public DBConnMgr(ConfigFile C) { this(C.getInt("SQLMaxConnections"),C); } public DBConnMgr(int M, ConfigFile C) { Config=C; MaxConn=M; Ready=new LinkedList(); NotOpen=new LinkedList(); for (int i=0; i 0) { Integer Index=(Integer)NotOpen.getFirst(); NotOpen.removeFirst(); SQLConnection NewConn=new SQLConnection(Config); if (NewConn.ok()) { Ready.addLast(NewConn); } else { System.out.println("DBConnMgr failed to open new connection"); NotOpen.addLast(Index); } } } public synchronized SQLConnection getConn() { int OpenFailures=0; while (Ready.size() > 0) { SQLConnection S=(SQLConnection)Ready.getFirst(); Ready.removeFirst(); S.Reset(); if (S.ok()) { return S; } else { NotOpen.add(new Integer(8)); } } while ((Ready.size() == 0) && (OpenFailures < Config.getInt("SQLMaxOpenTries"))) { if (NotOpen.size() > 0) { if (OpenFailures>0) { try { Thread.sleep(Config.getInt("SQLBackOff")); } catch (InterruptedException e) { } } Open(); if (Ready.size() == 0) { OpenFailures++; } } else { try { this.wait(); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); } } } while (Ready.size() > 0) { SQLConnection S=(SQLConnection)Ready.getFirst(); Ready.removeFirst(); S.Reset(); if (S.ok()) { return S; } else { NotOpen.add(new Integer(8)); } } return null; } public synchronized void relConn(SQLConnection S) { if (S!=null) { if (S.ok()) { //Just close the DB until this memory thing //is handled. //S.close(); //NotOpen.addLast(new Integer(-1)); S.Reset(); Ready.addLast(S); } else { NotOpen.addLast(new Integer(-1)); } this.notify(); } } public synchronized void close() { for(Iterator I=Ready.iterator(); I.hasNext(); ) { SQLConnection S=(SQLConnection)I.next(); S.close(); } Ready=new LinkedList(); NotOpen=new LinkedList(); for (int i=0; i