001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.dbcp;
019:
020: import java.text.SimpleDateFormat;
021: import java.util.ArrayList;
022: import java.util.Date;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: /**
027: * Tracks db connection usage for recovering and reporting
028: * abandoned db connections.
029: *
030: * The JDBC Connection, Statement, and ResultSet classes
031: * extend this class.
032: *
033: * @author Glenn L. Nielsen
034: * @version $Revision: 482015 $ $Date: 2006-12-03 19:22:09 -0700 (Sun, 03 Dec 2006) $
035: * @deprecated This will be removed in a future version of DBCP.
036: */
037: public class AbandonedTrace {
038:
039: /** Date format */
040: private static SimpleDateFormat format = new SimpleDateFormat(
041: "'DBCP object created' yyyy-MM-dd HH:mm:ss "
042: + "'by the following code was never closed:'");
043:
044: /** DBCP AbandonedConfig */
045: private AbandonedConfig config = null;
046: /** Parent object */
047: private AbandonedTrace parent;
048: /** A stack trace of the code that created me (if in debug mode) */
049: private Exception createdBy;
050: /** Time created */
051: private long createdTime;
052: /** A list of objects created by children of this object */
053: private List trace = new ArrayList();
054: /** Last time this connection was used */
055: private long lastUsed = 0;
056:
057: /**
058: * Create a new AbandonedTrace without config and
059: * without doing abandoned tracing.
060: */
061: public AbandonedTrace() {
062: init(parent);
063: }
064:
065: /**
066: * Construct a new AbandonedTrace with no parent object.
067: *
068: * @param config AbandonedConfig
069: */
070: public AbandonedTrace(AbandonedConfig config) {
071: this .config = config;
072: init(parent);
073: }
074:
075: /**
076: * Construct a new AbandonedTrace with a parent object.
077: *
078: * @param parent AbandonedTrace parent object
079: */
080: public AbandonedTrace(AbandonedTrace parent) {
081: this .config = parent.getConfig();
082: init(parent);
083: }
084:
085: /**
086: * Initialize abandoned tracing for this object.
087: *
088: * @param parent AbandonedTrace parent object
089: */
090: private void init(AbandonedTrace parent) {
091: if (parent != null) {
092: parent.addTrace(this );
093: }
094:
095: if (config == null) {
096: return;
097: }
098: if (config.getLogAbandoned()) {
099: createdBy = new Exception();
100: createdTime = System.currentTimeMillis();
101: }
102: }
103:
104: /**
105: * Get the abandoned config for this object.
106: *
107: * @return AbandonedConfig for this object
108: */
109: protected AbandonedConfig getConfig() {
110: return config;
111: }
112:
113: /**
114: * Get the last time this object was used in ms.
115: *
116: * @return long time in ms
117: */
118: protected long getLastUsed() {
119: if (parent != null) {
120: return parent.getLastUsed();
121: }
122: return lastUsed;
123: }
124:
125: /**
126: * Set the time this object was last used to the
127: * current time in ms.
128: */
129: protected void setLastUsed() {
130: if (parent != null) {
131: parent.setLastUsed();
132: } else {
133: lastUsed = System.currentTimeMillis();
134: }
135: }
136:
137: /**
138: * Set the time in ms this object was last used.
139: *
140: * @param time time in ms
141: */
142: protected void setLastUsed(long time) {
143: if (parent != null) {
144: parent.setLastUsed(time);
145: } else {
146: lastUsed = time;
147: }
148: }
149:
150: /**
151: * If logAbandoned=true generate a stack trace
152: * for this object then add this object to the parent
153: * object trace list.
154: */
155: protected void setStackTrace() {
156: if (config == null) {
157: return;
158: }
159: if (config.getLogAbandoned()) {
160: createdBy = new Exception();
161: createdTime = System.currentTimeMillis();
162: }
163: if (parent != null) {
164: parent.addTrace(this );
165: }
166: }
167:
168: /**
169: * Add an object to the list of objects being
170: * traced.
171: *
172: * @param trace AbandonedTrace object to add
173: */
174: protected void addTrace(AbandonedTrace trace) {
175: synchronized (this ) {
176: this .trace.add(trace);
177: }
178: setLastUsed();
179: }
180:
181: /**
182: * Clear the list of objects being traced by this
183: * object.
184: */
185: protected synchronized void clearTrace() {
186: if (this .trace != null) {
187: this .trace.clear();
188: }
189: }
190:
191: /**
192: * Get a list of objects being traced by this object.
193: *
194: * @return List of objects
195: */
196: protected List getTrace() {
197: return trace;
198: }
199:
200: /**
201: * If logAbandoned=true, print a stack trace of the code that
202: * created this object.
203: */
204: public void printStackTrace() {
205: if (createdBy != null) {
206: System.out.println(format.format(new Date(createdTime)));
207: createdBy.printStackTrace(System.out);
208: }
209: synchronized (this ) {
210: Iterator it = this .trace.iterator();
211: while (it.hasNext()) {
212: AbandonedTrace at = (AbandonedTrace) it.next();
213: at.printStackTrace();
214: }
215: }
216: }
217:
218: /**
219: * Remove a child object this object is tracing.
220: *
221: * @param trace AbandonedTrace object to remvoe
222: */
223: protected synchronized void removeTrace(AbandonedTrace trace) {
224: if (this.trace != null) {
225: this.trace.remove(trace);
226: }
227: }
228:
229: }
|