KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > AbandonedTrace


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.dbcp;
18
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * Tracks db connection usage for recovering and reporting
27  * abandoned db connections.
28  *
29  * The JDBC Connection, Statement, and ResultSet classes
30  * extend this class.
31  *
32  * @author Glenn L. Nielsen
33  * @version $Revision: 1.12 $ $Date: 2004/04/25 10:36:24 $
34  * @deprecated This will be removed in a future version of DBCP.
35  */

36 public class AbandonedTrace {
37
38     private static SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc
39         ("'DBCP object created' yyyy-MM-dd HH:mm:ss " +
40          "'by the following code was never closed:'");
41
42     // DBCP AbandonedConfig
43
private AbandonedConfig config = null;
44     // Parent object
45
private AbandonedTrace parent;
46     // A stack trace of the code that created me (if in debug mode) **/
47
private Exception JavaDoc createdBy;
48     private long createdTime;
49     // A list of objects created by children of this object
50
private List JavaDoc trace = new ArrayList JavaDoc();
51     // Last time this connection was used
52
private long lastUsed = 0;
53
54     /**
55      * Create a new AbandonedTrace without config and
56      * without doing abandoned tracing.
57      */

58     public AbandonedTrace() {
59         init(parent);
60     }
61
62     /**
63      * Construct a new AbandonedTrace with no parent object.
64      *
65      * @param AbandonedConfig
66      */

67     public AbandonedTrace(AbandonedConfig config) {
68         this.config = config;
69         init(parent);
70     }
71
72     /**
73      * Construct a new AbandonedTrace with a parent object.
74      *
75      * @param AbandonedTrace parent object
76      */

77     public AbandonedTrace(AbandonedTrace parent) {
78         this.config = parent.getConfig();
79         init(parent);
80     }
81
82     /**
83      * Initialize abandoned tracing for this object.
84      *
85      * @param AbandonedTrace parent object
86      */

87     private void init(AbandonedTrace parent) {
88         if (parent != null) {
89             parent.addTrace(this);
90         }
91
92         if (config == null) {
93             return;
94         }
95         if (config.getLogAbandoned()) {
96             createdBy = new Exception JavaDoc();
97             createdTime = System.currentTimeMillis();
98         }
99     }
100
101     /**
102      * Get the abandoned config for this object.
103      *
104      * @return AbandonedConfig for this object
105      */

106     protected AbandonedConfig getConfig() {
107         return config;
108     }
109
110     /**
111      * Get the last time this object was used in ms.
112      *
113      * @return long time in ms
114      */

115     protected long getLastUsed() {
116         if (parent != null) {
117            return parent.getLastUsed();
118         }
119         return lastUsed;
120     }
121
122     /**
123      * Set the time this object was last used to the
124      * current time in ms.
125      */

126     protected void setLastUsed() {
127         if (parent != null) {
128            parent.setLastUsed();
129         } else {
130            lastUsed = System.currentTimeMillis();
131         }
132     }
133
134     /**
135      * Set the time in ms this object was last used.
136      *
137      * @param long time in ms
138      */

139     protected void setLastUsed(long time) {
140         if (parent != null) {
141            parent.setLastUsed(time);
142         } else {
143            lastUsed = time;
144         }
145     }
146
147     /**
148      * If logAbandoned=true generate a stack trace
149      * for this object then add this object to the parent
150      * object trace list.
151      */

152     protected void setStackTrace() {
153         if (config == null) {
154             return;
155         }
156         if (config.getLogAbandoned()) {
157             createdBy = new Exception JavaDoc();
158             createdTime = System.currentTimeMillis();
159         }
160         if (parent != null) {
161             parent.addTrace(this);
162         }
163     }
164
165     /**
166      * Add an object to the list of objects being
167      * traced.
168      *
169      * @param AbandonedTrace object to add
170      */

171     protected void addTrace(AbandonedTrace trace) {
172         synchronized(this) {
173             this.trace.add(trace);
174         }
175         setLastUsed();
176     }
177
178     /**
179      * Clear the list of objects being traced by this
180      * object.
181      */

182     protected synchronized void clearTrace() {
183         if (this.trace != null) {
184             this.trace.clear();
185         }
186     }
187
188     /**
189      * Get a list of objects being traced by this object.
190      *
191      * @return List of objects
192      */

193     protected List JavaDoc getTrace() {
194         return trace;
195     }
196
197     /**
198      * If logAbandoned=true, print a stack trace of the code that
199      * created this object.
200      */

201     public void printStackTrace() {
202         if (createdBy != null) {
203             System.out.println(format.format(new Date JavaDoc(createdTime)));
204             createdBy.printStackTrace(System.out);
205         }
206         synchronized(this) {
207             Iterator JavaDoc it = this.trace.iterator();
208             while (it.hasNext()) {
209                 AbandonedTrace at = (AbandonedTrace)it.next();
210                 at.printStackTrace();
211             }
212         }
213     }
214
215     /**
216      * Remove a child object this object is tracing.
217      *
218      * @param AbandonedTrace object to remvoe
219      */

220     protected synchronized void removeTrace(AbandonedTrace trace) {
221         if (this.trace != null) {
222             this.trace.remove(trace);
223         }
224     }
225
226 }
227
Popular Tags