001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.commons.dsi;
020:
021: import java.sql.*;
022: import java.util.*;
023:
024: /**
025: * A singleton class which provides access to database connection pools. A single
026: * static method provides access to each instance, each instance then provides access to an
027: * individual pool for connections to any one database.
028: *
029: * @author Michael Bell
030: * @version $Revision: 1.1 $
031: *
032: */
033: public class DBConnectionPooler {
034:
035: /**
036: * Map of database connectin pools.
037: */
038: private static Map m_DBCPinstance_map = Collections
039: .synchronizedMap(new HashMap(50));
040:
041: /**
042: * Connection pool for this instance.
043: */
044: private DBConnectionPool m_broker = null;
045:
046: /**
047: * Constructs a pooler instance with the given database parameters.
048: *
049: * @param driver the JDBC driver class name
050: * @param dsn the database URI
051: * @param usr the database user name
052: * @param pwd the database user password
053: * @throws DataStoreException if an error occurs creating the pool
054: */
055: private DBConnectionPooler(String driver, String dsn, String usr,
056: String pwd) throws DataStoreException {
057: try {
058: m_broker = new DBConnectionPool(driver, dsn, usr, pwd);
059: } catch (Exception e) {
060: throw new DataStoreException(
061: "Fatal Error in Connection Pool,", e);
062: }
063: }
064:
065: /**
066: * Returns the number of connection in the pool.
067: *
068: * @return the number of connection in the pool
069: */
070: public int getNumConnections() {
071: return m_broker.countObjects();
072: }
073:
074: /**
075: * Returns a connection from the pool.
076: *
077: * @return a connection from the pool
078: * @throws SQLException if an error occurs
079: */
080: public Connection getConnection() throws SQLException {
081: return m_broker.borrowConnection();
082: }
083:
084: /**
085: * Returns the connection to the pool.
086: *
087: * @param conn the connection to be returned to the pool
088: */
089: public void freeConnection(Connection conn) {
090: m_broker.returnConnection(conn);
091: }
092:
093: /**
094: * Returns the instance of the pooler that corresponds to the given
095: * database parameters.
096: *
097: * @param driver the JDBC driver class name
098: * @param dsn the database URI
099: * @param usr the database user name
100: * @param pwd the database user password
101: * @return the instance of the pooler that corresponds to the given
102: * database parameters
103: * @throws DataStoreException if an error occurs creating the connection pool
104: */
105: synchronized public static DBConnectionPooler getInstance(
106: String driver, String dsn, String usr, String pwd)
107: throws DataStoreException {
108:
109: StringBuffer sbuf = new StringBuffer();
110:
111: sbuf.append(driver).append(":").append(dsn).append(":").append(
112: usr).append(":").append(pwd);
113:
114: String sMapKey = sbuf.toString();
115:
116: DBConnectionPooler DBCPinstance = (DBConnectionPooler) m_DBCPinstance_map
117: .get(sMapKey);
118:
119: if (DBCPinstance == null) {
120: DBCPinstance = new DBConnectionPooler(driver, dsn, usr, pwd);
121:
122: m_DBCPinstance_map.put(sMapKey, DBCPinstance);
123: }
124:
125: return DBCPinstance;
126: }
127: }
|