KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/util/Attic/JMeter19ConnectionPool.java,v 1.5 2004/02/12 00:33:56 sebb Exp $
2
/*
3  * Copyright 2003-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.sql.Connection JavaDoc;
22 import java.sql.DatabaseMetaData JavaDoc;
23 import java.sql.DriverManager JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler;
29 import org.apache.jmeter.testelement.property.JMeterProperty;
30 import org.apache.jorphan.logging.LoggingManager;
31 import org.apache.log.Logger;
32
33
34 /**
35  * @author <a HREF="mailto:jeremy_a@bigfoot.com">Jeremy Arnold</a>
36  * @version $Revision: 1.5 $
37  */

38 public class JMeter19ConnectionPool implements ConnectionPool
39 {
40     private static Logger log = LoggingManager.getLoggerForClass();
41     private static final int ABSOLUTE_MAX_CONNECTIONS = 100;
42
43     public static final String JavaDoc CONNECTIONS =
44         JDBCSampler.JDBCSAMPLER_PROPERTY_PREFIX + "connections";
45     public static final String JavaDoc MAXUSE =
46         JDBCSampler.JDBCSAMPLER_PROPERTY_PREFIX + "maxuse";
47
48     private final ConnectionObject connectionArray[];
49     private final Hashtable JavaDoc rentedConnections = new Hashtable JavaDoc();
50
51     private final DBKey key;
52     private int maxConnections;
53
54     public JMeter19ConnectionPool(DBKey key, Map JavaDoc properties)
55         throws ConnectionPoolException
56     {
57         this.key = key;
58
59         this.maxConnections =
60             ((JMeterProperty) properties.get(CONNECTIONS)).getIntValue();
61         int maxUsage =
62             ((JMeterProperty) properties.get(MAXUSE)).getIntValue();
63         
64         validateMaxConnections();
65
66         connectionArray = new ConnectionObject[maxConnections];
67         try
68         {
69             for (int i = 0; i < maxConnections; i++)
70             {
71                 connectionArray[i] = new ConnectionObject(key, maxUsage);
72             }
73         }
74         catch (SQLException JavaDoc e)
75         {
76             log.error("Error initializing JDBC connection pool", e);
77             throw new ConnectionPoolException(
78                 "Error initializing JDBC connection pool: " + e);
79         }
80     }
81
82
83     private void validateMaxConnections()
84     {
85         //Connection c = null;
86
try
87         {
88             DatabaseMetaData JavaDoc md =
89                 DriverManager
90                     .getConnection(
91                         key.getUrl(),
92                         key.getUsername(),
93                         key.getPassword())
94                     .getMetaData();
95
96             int dbMax = md.getMaxConnections();
97             if (dbMax > 0 && maxConnections > dbMax)
98             {
99                 log.warn(
100                     "Connection pool configured for "
101                         + maxConnections
102                         + " but database claims to allow only "
103                         + dbMax
104                         + ". Reducing the pool size, but problems may occur "
105                         + "if multiple connection pools are in use for the "
106                         + "same database.");
107                 maxConnections = dbMax;
108             }
109             else if (maxConnections > ABSOLUTE_MAX_CONNECTIONS)
110             {
111                 maxConnections = ABSOLUTE_MAX_CONNECTIONS;
112             }
113         }
114         catch (Exception JavaDoc e)
115         {
116             log.error("Couldn't get connection to database", e);
117             maxConnections = 0;
118             return;
119         }
120         finally
121         {
122 // if (c != null)
123
// {
124
// try
125
// {
126
// c.close();
127
// }
128
// catch (SQLException e)
129
// {
130
// log.warn("Error closing metadata database connection", e);
131
// }
132
// }
133
}
134     }
135
136     /**
137      * Rents out a database connection object.
138      * @return Connection object.
139      */

140     public Connection JavaDoc getConnection() throws ConnectionPoolException
141     {
142         if (connectionArray.length == 0)
143         {
144             throw new NoConnectionsAvailableException();
145         }
146
147         Connection JavaDoc c = null;
148         int attempts = 0;
149         while (attempts < 20 && (c = attemptGetConnection()) == null)
150         {
151             try
152             {
153                 Thread.sleep(10);
154             }
155             catch (Exception JavaDoc err)
156             {
157                 attempts++;
158             }
159         }
160         
161         return c;
162     }
163
164     private Connection JavaDoc attemptGetConnection()
165     {
166         Connection JavaDoc c = null;
167         int index = (int) (100 * Math.random());
168         int count = -1;
169         while (++count < maxConnections && c == null)
170         {
171             index++;
172             c = connectionArray[index % maxConnections].grab();
173         }
174         
175         if (c != null)
176         {
177             rentedConnections.put(c, connectionArray[index % maxConnections]);
178         }
179         
180         return c;
181     }
182     /**
183      * Releases a connection back to the pool.
184      * @param c Connection object being returned
185      */

186     public void returnConnection(Connection JavaDoc c)
187     {
188         if (c == null)
189         {
190             return;
191         }
192         
193         ConnectionObject connOb = (ConnectionObject) rentedConnections.get(c);
194         if (connOb != null)
195         {
196             rentedConnections.remove(c);
197             connOb.release();
198         }
199         else
200         {
201             log.warn("DBConnectionManager: Lost a connection connection='" + c);
202             c = null;
203         }
204     }
205
206     /**
207      * Returns a new java.sql.Connection object.
208      */

209     public Connection JavaDoc newConnection(DBKey key) throws SQLException JavaDoc
210     {
211         return DriverManager.getConnection(
212             key.getUrl(),
213             key.getUsername(),
214             key.getPassword());
215     }
216
217     /**
218      * Closes out this object and returns resources to the system.
219      */

220     public void close()
221     {
222         for (int i = 0; i < connectionArray.length; i++)
223         {
224             connectionArray[i].close();
225             connectionArray[i] = null;
226         }
227     }
228 }
229
Popular Tags