www.pudn.com > town-1[1].0.4.rar > DBConnection.java


package com.workingdogs.town; 
 
import java.io.*; 
import java.sql.*; 
import java.util.*; 
 
import com.javaexchange.dbConnectionBroker.*; 
 
/* 
Town, a Java JDBC abstraction layer 
Copyright (C) 1999  Serge Knystautas, Jon Stevens 
 
This library is free software; you can redistribute it and/or 
modify it under the terms of the GNU Library General Public 
License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version. 
 
This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
Library General Public License for more details. 
 
You should have received a copy of the GNU Library General Public 
License along with this library; if not, write to the 
Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
Boston, MA  02111-1307, USA. 
*/ 
 
/** 
 * This base class handles connection management, creating Connection objects from connection 
 * strings (using the connection pooler), and closing connections when complete.  Both the abstract 
 * DateSet class extends this, as well as the simple ExecuteStatement class. 
 * @author Serge Knystautas sergek@lokitech.com 
 * @version 1.0 
 */ 
public abstract class DBConnection 
{ 
    /** the Statement for this DBConnection */ 
    protected Statement stmt = null; 
    /** this DBConnection's connection object */ 
    protected Connection dbconn = null; 
 
    /** flag of whether this DataSet has been closed */ 
    private boolean closed = false; 
    /** this is the connection's driver */ 
    private String driver = null; 
    /** this is the connection's JDBC connection string */ 
    private String connString = null; 
    /** this is the connection's username */ 
    private String username = null; 
    /** this is the connection's password */ 
    private String password = null; 
    /** this is the broker object for this driver, connString, username, and password */ 
    private DbConnectionBroker broker = null; 
 
    private static Hashtable brokers = new Hashtable (); 
    /** 
     * DBConnection constructor comment. 
     */ 
    public DBConnection(String driver, 
            String connString) throws DataSetException, ConnectionException 
    { 
        this (driver, connString, null, null); 
    } 
    /** 
      * DBConnection constructor comment. 
      */ 
    public DBConnection(String driver, String connString, 
            String username, String password) throws DataSetException, 
    ConnectionException 
    { 
        this.driver = driver; 
        this.connString = connString; 
        this.username = username; 
        this.password = password; 
 
        if (driver == null) 
            throw new DataSetException ("You need to specify a valid driver!"); 
 
        if (connString == null) 
            throw new DataSetException ("You need to specify a valid JDBC connection string!"); 
 
        String key = (driver + '\t' + connString).toLowerCase (); 
 
        if (username != null) 
            key += '\t' + username.toLowerCase (); 
        if (password != null) 
            key += '\t' + password.toLowerCase (); 
 
        //	synchronized (brokers) 
        //	{ 
        if (brokers.get (key) != null) 
            broker = (DbConnectionBroker) brokers.get (key); 
        else 
        { 
            // The below statement sets up a Broker with a minimun pool size of 2 connections 
            // and a maximum of 5.  The log file will be created in 
            // D:\JavaWebServer1.1\DCB_Example.log and the pool connections will be 
            // restarted once a day. 
            try 
            { 
                broker = new DbConnectionBroker(driver, connString, 
                        username, password, 2, 6, "db_pool.log", 1); 
            } 
            catch (IOException ioe) 
            { 
                throw new ConnectionException (ioe); 
            } 
            brokers.put (key, broker); 
        } 
        //	} 
 
        dbconn = broker.getConnection (); 
    } 
    /** 
       * Releases the records, closes the ResultSet and the Statement and nulls the schema 
       * 
       * @exception   ConnectionException 
       * @exception   DataSetException 
       */ 
    public void close() throws ConnectionException, DataSetException 
    { 
        try 
        { 
            this.stmt.close(); 
 
        } 
        catch (SQLException sqle) 
        { 
            throw new ConnectionException (sqle); 
        } 
        catch (NullPointerException npe) 
            {} 
        finally { broker.freeConnection (dbconn); 
            closed = true; 
        } } 
    /** 
       * Gets the current database connection 
       * 
       * @return     a database connection 
       * @exception   ConnectionException 
       */ 
    protected Connection connection() throws ConnectionException 
    { 
        return dbconn; 
    } 
    /** 
      * This method was created in VisualAge. 
      */ 
    protected void finalize () throws Throwable 
    { 
        if (!closed) 
            close (); 
    } 
}