KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > jdbc > util > DBConnectionManager


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/util/Attic/DBConnectionManager.java,v 1.12 2004/02/26 00:28:47 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.jdbc.util;
20
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.sql.Connection JavaDoc;
23 import java.sql.Driver JavaDoc;
24 import java.sql.DriverManager JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30
31 import org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler;
32 import org.apache.jmeter.testelement.property.JMeterProperty;
33 import org.apache.jorphan.logging.LoggingManager;
34 import org.apache.log.Logger;
35
36 /**
37  * This class manages a pool of Connection objects (ConnectionObject). This
38  * pool is constantly checked for old, over-used, or dead connections in a
39  * separated thread. Connections are rented out and then given back by the
40  * DBConnect object and its subclasses. This class is not directly accessed
41  * by the end-user objects. It is accessed by the DBConnect object and its
42  * subclasses.
43  *
44  * @author Michael Stover
45  * @author <a HREF="mailto:jeremy_a@bigfoot.com">Jeremy Arnold</a>
46  * @version $Revision: 1.12 $
47  */

48
49 public final class DBConnectionManager
50 {
51     private static Logger log = LoggingManager.getLoggerForClass();
52
53     private static DBConnectionManager manager = new DBConnectionManager();
54     
55     /** Map of DBKey to ConnectionPool. */
56     private Map JavaDoc poolMap = new HashMap JavaDoc();
57     
58     /**
59      * Private constructor to prevent instantiation from outside this class.
60      */

61     private DBConnectionManager()
62     {
63     }
64
65     public static DBConnectionManager getManager()
66     {
67         return manager;
68     }
69
70     /**
71      * Starts the connection manager going for a given database connection, and
72      * returns the DBKey object required to get a Connection object for this
73      * database.
74      *
75      * @param url URL of database to be connected to.
76      * @param username username to use to connect to database.
77      * @param password password to use to connect to database.
78      * @param driver driver to use for the database.
79      * @param properties configuration properties to be used by the connection
80      * pool.
81      * @return DBKey object. Returns null if connection fails.
82      */

83     public DBKey getKey(
84         String JavaDoc url,
85         String JavaDoc username,
86         String JavaDoc password,
87         String JavaDoc driver,
88         Map JavaDoc properties)
89         throws ConnectionPoolException
90     {
91         DBKey key =
92             new DBKey(
93                 driver,
94                 url,
95                 username,
96                 password);
97
98         synchronized (poolMap)
99         {
100             if (!poolMap.containsKey(key))
101             {
102                 if (registerDriver(driver))
103                 {
104                     poolMap.put(
105                         key,
106                         createConnectionPool(key, properties));
107                 }
108                 else
109                 {
110                     return null;
111                 }
112             }
113         }
114         
115         return key;
116     }
117
118     private ConnectionPool createConnectionPool(DBKey key, Map JavaDoc properties)
119         throws ConnectionPoolException
120     {
121         String JavaDoc className =
122             ((JMeterProperty) properties.get(JDBCSampler.CONNECTION_POOL_IMPL))
123                 .getStringValue();
124
125         Class JavaDoc types[] = new Class JavaDoc[] {DBKey.class, Map JavaDoc.class};
126         Object JavaDoc params[] = new Object JavaDoc[] {key, properties};
127         
128         try
129         {
130             Class JavaDoc poolClass = Class.forName(className);
131             Constructor JavaDoc constructor = poolClass.getConstructor(types);
132             return (ConnectionPool)constructor.newInstance(params);
133         }
134         catch (Exception JavaDoc e)
135         {
136             log.error(
137                 "Error instantiating JDBC connection pool class '"
138                     + className
139                     + "'",
140                 e);
141             throw new ConnectionPoolException(
142                 "Error instantiating JDBC connection pool class '"
143                     + className
144                     + "': "
145                     + e);
146         }
147     }
148
149     public void shutdown()
150     {
151         log.debug("Running shutdown from "+Thread.currentThread().getName());
152         synchronized (poolMap)
153         {
154             Iterator JavaDoc iter = poolMap.keySet().iterator();
155             while (iter.hasNext())
156             {
157                 DBKey key = (DBKey)iter.next();
158                 ConnectionPool pool = (ConnectionPool) poolMap.remove(key);
159                 pool.close();
160             }
161         }
162     }
163
164     private ConnectionPool getPool(DBKey key)
165     {
166         synchronized (poolMap)
167         {
168             return (ConnectionPool)poolMap.get(key);
169         }
170     }
171     
172     /**
173      * Rents out a database connection object.
174      * @return Connection object.
175      */

176     public Connection JavaDoc getConnection(DBKey key)
177         throws NoConnectionsAvailableException
178     {
179         ConnectionPool pool = getPool(key);
180         
181         if (pool == null)
182         {
183             throw new NoConnectionsAvailableException();
184         }
185         else
186         {
187             try
188             {
189                 return pool.getConnection();
190             }
191             catch (NoSuchElementException JavaDoc e)
192             {
193                 log.warn(
194                     "NoSuchElementException getting database connection",
195                     e);
196                 return null;
197             }
198             catch (Exception JavaDoc e)
199             {
200                 log.warn("Exception getting database connection from pool", e);
201                 throw new NoConnectionsAvailableException();
202             }
203         }
204     }
205
206     /**
207      * Releases a connection back to the pool.
208      * @param c Connection object being returned
209      */

210     public void releaseConnection(DBKey key, Connection JavaDoc c)
211     {
212         getPool(key).returnConnection(c);
213     }
214
215     /**
216      * Registers a driver for a database.
217      * @param driver full classname for the driver.
218      * @return True if successful, false otherwise.
219      */

220     private boolean registerDriver(String JavaDoc driver)
221     {
222         try
223         {
224             DriverManager.registerDriver(
225                 (Driver JavaDoc) Class.forName(driver).newInstance());
226         }
227         catch (SQLException JavaDoc e)
228         {
229             log.error("Error registering database driver '" + driver + "'", e);
230             return false;
231         } catch (InstantiationException JavaDoc e) {
232             log.error("Error registering database driver '" + driver + "'", e);
233             return false;
234         } catch (IllegalAccessException JavaDoc e) {
235             log.error("Error registering database driver '" + driver + "'", e);
236             return false;
237         } catch (ClassNotFoundException JavaDoc e) {
238             log.error("Error registering database driver '" + driver + "'", e);
239             return false;
240         }
241         return true;
242     }
243 }
244
Popular Tags