KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > commons > dsi > DBConnectionPooler


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.commons.dsi;
20
21
22 import java.sql.*;
23 import java.util.*;
24
25
26 /**
27  * A singleton class which provides access to database connection pools. A single
28  * static method provides access to each instance, each instance then provides access to an
29  * individual pool for connections to any one database.
30  *
31  * @author Michael Bell
32  * @version $Revision: 1.1 $
33  *
34  */

35 public class DBConnectionPooler {
36     
37     /**
38      * Map of database connectin pools.
39      */

40     private static Map m_DBCPinstance_map = Collections.synchronizedMap(new HashMap(50));
41     
42     /**
43      * Connection pool for this instance.
44      */

45     private DBConnectionPool m_broker = null;
46
47     /**
48      * Constructs a pooler instance with the given database parameters.
49      *
50      * @param driver the JDBC driver class name
51      * @param dsn the database URI
52      * @param usr the database user name
53      * @param pwd the database user password
54      * @throws DataStoreException if an error occurs creating the pool
55      */

56     private DBConnectionPooler(String JavaDoc driver, String JavaDoc dsn, String JavaDoc usr, String JavaDoc pwd) throws DataStoreException {
57         try {
58             m_broker = new DBConnectionPool(driver,
59                                                 dsn,
60                                                 usr,
61                                                 pwd);
62         } catch (Exception JavaDoc e) {
63             throw new DataStoreException("Fatal Error in Connection Pool,",e);
64         }
65     }
66
67     /**
68      * Returns the number of connection in the pool.
69      *
70      * @return the number of connection in the pool
71      */

72     public int getNumConnections() {
73         return m_broker.countObjects();
74     }
75
76     /**
77      * Returns a connection from the pool.
78      *
79      * @return a connection from the pool
80      * @throws SQLException if an error occurs
81      */

82     public Connection getConnection() throws SQLException {
83         return m_broker.borrowConnection();
84     }
85
86     /**
87      * Returns the connection to the pool.
88      *
89      * @param conn the connection to be returned to the pool
90      */

91     public void freeConnection(Connection conn) {
92         m_broker.returnConnection(conn);
93     }
94
95     /**
96      * Returns the instance of the pooler that corresponds to the given
97      * database parameters.
98      *
99      * @param driver the JDBC driver class name
100      * @param dsn the database URI
101      * @param usr the database user name
102      * @param pwd the database user password
103      * @return the instance of the pooler that corresponds to the given
104      * database parameters
105      * @throws DataStoreException if an error occurs creating the connection pool
106      */

107     synchronized public static DBConnectionPooler getInstance(String JavaDoc driver, String JavaDoc dsn, String JavaDoc usr,
108     String JavaDoc pwd)
109         throws DataStoreException {
110             
111         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
112         
113         sbuf.append(driver).append(":").append(dsn).append(":").append(usr).append(":").append(pwd);
114         
115         String JavaDoc sMapKey = sbuf.toString();
116         
117         DBConnectionPooler DBCPinstance = (DBConnectionPooler)m_DBCPinstance_map.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 }
Popular Tags