You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by gl...@apache.org on 2002/06/23 23:41:43 UTC
cvs commit: jakarta-commons/dbcp/src/java/org/apache/commons/dbcp AbandonedTrace.java AbandonedObjectPool.java
glenn 2002/06/23 14:41:42
Modified: dbcp/src/java/org/apache/commons/dbcp AbandonedTrace.java
AbandonedObjectPool.java
Log:
FastArrayList wasn't useful, switched to synchronized methods to fix thread bug
Revision Changes Path
1.3 +10 -13 jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/AbandonedTrace.java
Index: AbandonedTrace.java
===================================================================
RCS file: /home/cvs/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/AbandonedTrace.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AbandonedTrace.java 20 Jun 2002 00:16:51 -0000 1.2
+++ AbandonedTrace.java 23 Jun 2002 21:41:42 -0000 1.3
@@ -61,12 +61,11 @@
package org.apache.commons.dbcp;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
-import org.apache.commons.collections.FastArrayList;
-
/**
* Tracks db connection usage for recovering and reporting
* abandoned db connections.
@@ -91,7 +90,7 @@
private Exception createdBy;
private Date createdDate;
// A list of objects created by children of this object
- private List trace = new FastArrayList();
+ private List trace = new ArrayList();
// Last time this connection was used
private long lastUsed = 0;
@@ -132,7 +131,6 @@
if (parent != null) {
parent.addTrace(this);
}
- ((FastArrayList)trace).setFast(true);
if (config == null) {
return;
@@ -213,7 +211,7 @@
*
* @param AbandonedTrace object to add
*/
- protected void addTrace(AbandonedTrace trace) {
+ protected synchronized void addTrace(AbandonedTrace trace) {
this.trace.add(trace);
setLastUsed();
}
@@ -222,7 +220,7 @@
* Clear the list of objects being traced by this
* object.
*/
- protected void clearTrace() {
+ protected synchronized void clearTrace() {
if (trace != null) {
trace.clear();
}
@@ -241,7 +239,7 @@
* If logAbandoned=true, print a stack trace of the code that
* created this object.
*/
- public void printStackTrace() {
+ public synchronized void printStackTrace() {
if (createdBy != null) {
System.out.println(format.format(createdDate));
createdBy.printStackTrace();
@@ -258,11 +256,10 @@
*
* @param AbandonedTrace object to remvoe
*/
- protected void removeTrace(AbandonedTrace trace) {
+ protected synchronized void removeTrace(AbandonedTrace trace) {
if (this.trace != null) {
this.trace.remove(trace);
}
}
}
-
1.2 +44 -28 jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/AbandonedObjectPool.java
Index: AbandonedObjectPool.java
===================================================================
RCS file: /home/cvs/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/AbandonedObjectPool.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbandonedObjectPool.java 16 May 2002 21:25:37 -0000 1.1
+++ AbandonedObjectPool.java 23 Jun 2002 21:41:42 -0000 1.2
@@ -61,11 +61,12 @@
package org.apache.commons.dbcp;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
+import java.util.NoSuchElementException;
-import org.apache.commons.collections.FastArrayList;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
@@ -83,7 +84,7 @@
// DBCP AbandonedConfig
private AbandonedConfig config = null;
// A list of connections in use
- private List trace = new FastArrayList();
+ private List trace = new ArrayList();
/**
* Create an ObjectPool which tracks db connections.
@@ -95,7 +96,6 @@
AbandonedConfig config) {
super(factory);
this.config = config;
- ((FastArrayList)trace).setFast(true);
}
/**
@@ -106,18 +106,26 @@
*
* @return Object jdbc Connection
*/
- public Object borrowObject() throws Exception {
- if (config != null
- && config.getRemoveAbandoned()
- && (getNumIdle() < 2)
- && (getNumActive() > getMaxActive() - 3) ) {
- removeAbandoned();
- }
- Object obj = super.borrowObject();
- if (obj != null && config != null && config.getRemoveAbandoned()) {
- trace.add(obj);
+ public synchronized Object borrowObject() throws Exception {
+ try {
+ if (config != null
+ && config.getRemoveAbandoned()
+ && (getNumIdle() < 2)
+ && (getNumActive() > getMaxActive() - 3) ) {
+ removeAbandoned();
+ }
+ Object obj = super.borrowObject();
+ if (obj != null && config != null && config.getRemoveAbandoned()) {
+ trace.add(obj);
+ }
+ return obj;
+ } catch(NoSuchElementException ne) {
+ throw new SQLException(
+ "DBCP could not obtain an idle db connection, pool exhausted");
+ } catch(Exception e) {
+ System.out.println("DBCP borrowObject failed: " + e.getMessage());
+ throw e;
}
- return obj;
}
/**
@@ -125,7 +133,7 @@
*
* @param Object db Connection to return
*/
- public void returnObject(Object obj) throws Exception {
+ public synchronized void returnObject(Object obj) throws Exception {
if (config != null && config.getRemoveAbandoned()) {
trace.remove(obj);
}
@@ -137,24 +145,32 @@
* greater than the removeAbandonedTimeout.
*/
private synchronized void removeAbandoned() {
+ // Generate a list of abandoned connections to remove
long now = new Date().getTime();
long timeout = now - (config.getRemoveAbandonedTimeout() * 1000);
+ ArrayList remove = new ArrayList();
Iterator it = trace.iterator();
while (it.hasNext()) {
PoolableConnection pc = (PoolableConnection)it.next();
if (pc.getLastUsed() > timeout) {
- return;
+ continue;
}
if (pc.getLastUsed() > 0) {
- if (config.getLogAbandoned()) {
- pc.printStackTrace();
- }
- try {
- pc.close();
- } catch(SQLException e) {
- e.printStackTrace();
- }
+ remove.add(pc);
}
+ }
+ // Now remove the abandoned connections
+ it = remove.iterator();
+ while (it.hasNext()) {
+ PoolableConnection pc = (PoolableConnection)it.next();
+ if (config.getLogAbandoned()) {
+ pc.printStackTrace();
+ }
+ try {
+ pc.close();
+ } catch(SQLException e) {
+ e.printStackTrace();
+ }
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>