KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/util/Attic/ConnectionObject.java,v 1.7.2.1 2004/06/12 20:29:25 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.sql.Connection JavaDoc;
22 import java.sql.DriverManager JavaDoc;
23 import java.sql.SQLException JavaDoc;
24
25 import org.apache.jorphan.logging.LoggingManager;
26 import org.apache.log.Logger;
27
28 /**
29  * A wrapper class for a database Connection object. This class holds
30  * information about the state of the connection object in a pool managed by
31  * a DBConnectionManager object. It keeps track of how many times the
32  * connection object has been used, the time of its last usage, and whether it
33  * is currently in use.
34  * @author Michael Stover
35  * @version $Revision: 1.7.2.1 $
36  */

37 public class ConnectionObject implements Runnable JavaDoc
38 {
39     static long accessInterval = 180000;
40
41     private static Logger log = LoggingManager.getLoggerForClass();
42
43     private final DBKey key;
44     private Connection JavaDoc con;
45     private int useCount;
46     private int maxUsage;
47     private long lastAccessed;
48     private volatile boolean inUse;
49     private volatile boolean inMaintenance;
50
51     private Thread JavaDoc reset;
52     
53     /**
54      * Constructor - takes a connection object.
55      *
56      * @param k DBKey object.
57      * @param maxUsage
58      */

59     public ConnectionObject(DBKey k, int maxUsage) throws SQLException JavaDoc
60     {
61         key = k;
62         reset = new Thread JavaDoc(this);
63         useCount = 0;
64         lastAccessed = System.currentTimeMillis();
65         inMaintenance = true;
66         inUse = false;
67         con = null;
68         this.maxUsage = maxUsage;
69         reset();
70     }
71
72     /**
73      * Gets whether the Connection Object is being maintained.
74      * @return true if the ConnectionObject is being maintained, false
75      * otherwise.
76      */

77     public boolean getInMaintenance()
78     {
79         return inMaintenance;
80     }
81
82     /**
83      * Sets whether the Connection Object is being maintained.
84      * @param b true if the ConnectionObject is being maintained, false
85      * otherwise.
86      */

87     public void setInMaintenance(boolean b)
88     {
89         inMaintenance = b;
90     }
91
92     /**
93      * Gets the last time this object was accessed.
94      * @return Time (in milliseconds) the connection object was last used
95      */

96     public long getLastAccessed()
97     {
98         return lastAccessed;
99     }
100
101     /**
102      * Closes out this object and returns resources to the system.
103      */

104     public void close()
105     {
106         if (con != null)
107         {
108             try
109             {
110                 con.close();
111             }
112             catch (SQLException JavaDoc e)
113             {
114             }
115         }
116         con = null;
117     }
118
119     /**
120      * Updates the last accessed time for the connection object.
121      */

122     public void update()
123     {
124         lastAccessed = System.currentTimeMillis();
125     }
126
127     /**
128      * Gets whether the connection object is currently in use.
129      * @return True if it is in use, false otherwise.
130      */

131     public boolean getInUse()
132     {
133         return inUse;
134     }
135
136     /**
137      * Grabs the connection and sets the inUse value to true.
138      * @return connection object
139      */

140     public synchronized Connection JavaDoc grab()
141     {
142         Connection JavaDoc c = null;
143         if (inUse || inMaintenance)
144         {
145             log.debug(
146                 "Connection not available because it is "
147                     + (inUse ? "in use" : "in maintenance"));
148         }
149         else
150         {
151             if (con != null)
152             {
153                 try
154                 {
155                     if (con.isClosed())
156                     {
157                         log.debug(
158                             "Connection is closed. "
159                                 + "Putting it in maintenance.");
160                         inMaintenance = true;
161                         // reset=new Thread(this);
162
reset.start();
163                     }
164                     else if (
165                         System.currentTimeMillis() - lastAccessed
166                             > accessInterval)
167                     {
168                         log.debug(
169                             "Connection is timed out. "
170                                 + "Putting it in maintenance.");
171                         inMaintenance = true;
172                         // reset=new Thread(this);
173
reset.start();
174                     }
175                     else
176                     {
177                         log.debug(
178                             "Connection is available."
179                                 + "Marking it as in use.");
180                         inUse = true;
181                         c = con;
182                     }
183                 }
184                 catch (SQLException JavaDoc e)
185                 {
186                     log.warn("Exception checking if connection is closed", e);
187                 }
188             }
189             else
190             {
191                 log.debug("connection is null. Putting it in maintenance.");
192                 inMaintenance = true;
193                 reset.start();
194             }
195         }
196         return c;
197     }
198
199     /**
200      * Gets the number of times this connection object has been used.
201      * @return Number of times the connection object has been used.
202      */

203     public int getUseCount()
204     {
205         return useCount;
206     }
207
208     /**
209      * Method to run in separate thread that resets the connection object
210      */

211     public void run()
212     {
213         boolean set = true;
214         while (set)
215         {
216             try
217             {
218                 reset();
219                 set = false;
220             }
221             catch (SQLException JavaDoc e)
222             {
223                 log.error("ConnectionObject: url = " + key.getUrl(), e);
224             }
225         }
226         reset = new Thread JavaDoc(this);
227     }
228
229     /**
230      * Resets the use count, the last accessed time, and the in Use values
231      * and replaces the old connection object with the new one.
232      */

233     public void reset() throws SQLException JavaDoc
234     {
235         if (con != null)
236         {
237             try
238             {
239                 con.close();
240             }
241             catch (Exception JavaDoc e)
242             {
243             }
244             con = null;
245         }
246         con = DriverManager.getConnection(
247             key.getUrl(),
248             key.getUsername(),
249             key.getPassword());
250
251         useCount = 0;
252         lastAccessed = System.currentTimeMillis();
253         inUse = false;
254         inMaintenance = false;
255     }
256
257     /**
258      * Releases the connection object. Increments its usage count, updates
259      * the last accessed time, and returns it for use in the pool.
260      */

261     public void release()
262     {
263         useCount++;
264         try
265         {
266             if (con != null)
267             {
268                 if (System.currentTimeMillis() - lastAccessed > accessInterval)
269                 {
270                     inMaintenance = true;
271                     reset.start();
272                 }
273                 else if (useCount >= maxUsage)
274                 {
275                     inMaintenance = true;
276                     reset.start();
277                 }
278                 else if (con.isClosed())
279                 {
280                     inMaintenance = true;
281                     reset.start();
282                 }
283             }
284             else
285             {
286                 inMaintenance = true;
287                 reset.start();
288             }
289         }
290         catch (SQLException JavaDoc e)
291         {
292             inMaintenance = true;
293             reset.start();
294         }
295         inUse = false;
296         lastAccessed = System.currentTimeMillis();
297     }
298
299     /**
300      * Returns the connection held by this connection object.
301      * @return Connection object
302      */

303     public Connection JavaDoc getCon()
304     {
305         return con;
306     }
307 }
308
Popular Tags