KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.commons.pool.PoolableObjectFactory;
24 import org.apache.commons.pool.impl.GenericObjectPool;
25
26 /**
27  * <p>An implementation of a Jakarta-Commons ObjectPool which
28  * tracks JDBC connections and can recover abandoned db connections.
29  * If logAbandoned=true, a stack trace will be printed for any
30  * abandoned db connections recovered.
31  *
32  * @author Glenn L. Nielsen
33  * @version $Revision: 1.16 $ $Date: 2004/05/01 12:42:19 $
34  * @deprecated This will be removed in a future version of DBCP.
35  */

36 public class AbandonedObjectPool extends GenericObjectPool {
37
38     // DBCP AbandonedConfig
39
private AbandonedConfig config = null;
40     // A list of connections in use
41
private List JavaDoc trace = new ArrayList JavaDoc();
42
43     /**
44      * Create an ObjectPool which tracks db connections.
45      *
46      * @param PoolableObjectFactory factory used to create this
47      * @param AbandonedConfig configuration for abandoned db connections
48      */

49     public AbandonedObjectPool(PoolableObjectFactory factory,
50                                AbandonedConfig config) {
51         super(factory);
52         this.config = config;
53         System.out.println("AbandonedObjectPool is used (" + this + ")");
54         System.out.println(" LogAbandoned: " + config.getLogAbandoned());
55         System.out.println(" RemoveAbandoned: " + config.getRemoveAbandoned());
56         System.out.println(" RemoveAbandonedTimeout: " + config.getRemoveAbandonedTimeout());
57     }
58
59     /**
60      * Get a db connection from the pool.
61      *
62      * If removeAbandoned=true, recovers db connections which
63      * have been idle > removeAbandonedTimeout.
64      *
65      * @return Object jdbc Connection
66      */

67     public Object JavaDoc borrowObject() throws Exception JavaDoc {
68         if (config != null
69                 && config.getRemoveAbandoned()
70                 && (getNumIdle() < 2)
71                 && (getNumActive() > getMaxActive() - 3) ) {
72             removeAbandoned();
73         }
74         Object JavaDoc obj = super.borrowObject();
75         if(obj instanceof AbandonedTrace) {
76             ((AbandonedTrace)obj).setStackTrace();
77         }
78         if (obj != null && config != null && config.getRemoveAbandoned()) {
79             synchronized(trace) {
80                 trace.add(obj);
81             }
82         }
83         return obj;
84     }
85
86     /**
87      * Return a db connection to the pool.
88      *
89      * @param Object db Connection to return
90      */

91     public void returnObject(Object JavaDoc obj) throws Exception JavaDoc {
92         if (config != null && config.getRemoveAbandoned()) {
93             synchronized(trace) {
94                 boolean foundObject = trace.remove(obj);
95                 if (!foundObject) {
96                     return; // This connection has already been invalidated. Stop now.
97
}
98             }
99         }
100         super.returnObject(obj);
101     }
102
103     public void invalidateObject(Object JavaDoc obj) throws Exception JavaDoc {
104         if (config != null && config.getRemoveAbandoned()) {
105             synchronized(trace) {
106                 boolean foundObject = trace.remove(obj);
107                 if (!foundObject) {
108                     return; // This connection has already been invalidated. Stop now.
109
}
110             }
111         }
112         super.invalidateObject(obj);
113     }
114
115     /**
116      * Recover abandoned db connections which have been idle
117      * greater than the removeAbandonedTimeout.
118      */

119     private void removeAbandoned() {
120         // Generate a list of abandoned connections to remove
121
long now = System.currentTimeMillis();
122         long timeout = now - (config.getRemoveAbandonedTimeout() * 1000);
123         ArrayList JavaDoc remove = new ArrayList JavaDoc();
124         synchronized(trace) {
125             Iterator JavaDoc it = trace.iterator();
126             while (it.hasNext()) {
127                 AbandonedTrace pc = (AbandonedTrace)it.next();
128                 if (pc.getLastUsed() > timeout) {
129                     continue;
130                 }
131                 if (pc.getLastUsed() > 0) {
132                     remove.add(pc);
133                 }
134             }
135         }
136
137         // Now remove the abandoned connections
138
Iterator JavaDoc it = remove.iterator();
139         while (it.hasNext()) {
140             AbandonedTrace pc = (AbandonedTrace)it.next();
141             if (config.getLogAbandoned()) {
142                 pc.printStackTrace();
143             }
144             try {
145                 invalidateObject(pc);
146             } catch(Exception JavaDoc e) {
147                 e.printStackTrace();
148             }
149         }
150     }
151 }
152
Popular Tags