commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1531310 - in /commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2: PooledObject.java impl/DefaultPooledObject.java impl/GenericObjectPool.java
Date Fri, 11 Oct 2013 14:31:06 GMT
Author: markt
Date: Fri Oct 11 14:31:05 2013
New Revision: 1531310

URL: http://svn.apache.org/r1531310
Log:
POOL-211
PooledObject#getLastUsed() -> PooledObject#getLastUsedTime() for consistency
Add PooledObject#use() to enable the last use to be tracked
Implement this method in DefaultPooledObject
Plumb in the use of this method to GenericObjectPool

Modified:
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PooledObject.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/DefaultPooledObject.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PooledObject.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PooledObject.java?rev=1531310&r1=1531309&r2=1531310&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PooledObject.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PooledObject.java Fri
Oct 11 14:31:05 2013
@@ -71,7 +71,7 @@ public interface PooledObject<T> extends
      *
      * @return the last time this object was used
      */
-    long getLastUsed();
+    long getLastUsedTime();
 
     /**
      * Orders instances based on idle time - i.e. the length of time since the
@@ -128,7 +128,13 @@ public interface PooledObject<T> extends
     void setLogAbandoned(boolean logAbandoned);
 
     /**
-     * Prints the stack trace of the code that borrowed this pooled object to
+     * Record the current stack trace as the last time the object was used.
+     */
+    void use();
+
+    /**
+     * Prints the stack trace of the code that borrowed this pooled object and
+     * the stack trace of the last code to use this object (if available) to
      * the supplied writer.
      */
     void printStackTrace(PrintWriter writer);

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/DefaultPooledObject.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/DefaultPooledObject.java?rev=1531310&r1=1531309&r2=1531310&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/DefaultPooledObject.java
(original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/DefaultPooledObject.java
Fri Oct 11 14:31:05 2013
@@ -43,9 +43,11 @@ public class DefaultPooledObject<T> impl
     private PooledObjectState state = PooledObjectState.IDLE; // @GuardedBy("this") to ensure
transitions are valid
     private final long createTime = System.currentTimeMillis();
     private volatile long lastBorrowTime = createTime;
+    private volatile long lastUseTime = createTime;
     private volatile long lastReturnTime = createTime;
     private volatile boolean logAbandoned = false;
     private Exception borrowedBy = null;
+    private Exception usedBy = null;
 
     public DefaultPooledObject(T object) {
         this.object = object;
@@ -117,11 +119,11 @@ public class DefaultPooledObject<T> impl
      * @return the last time this object was used
      */
     @Override
-    public long getLastUsed() {
+    public long getLastUsedTime() {
         if (object instanceof TrackedUse) {
-            return Math.max(((TrackedUse) object).getLastUsed(), lastBorrowTime);
+            return Math.max(((TrackedUse) object).getLastUsed(), lastUseTime);
         } else {
-            return lastBorrowTime;
+            return lastUseTime;
         }
     }
 
@@ -214,8 +216,9 @@ public class DefaultPooledObject<T> impl
         if (state == PooledObjectState.IDLE) {
             state = PooledObjectState.ALLOCATED;
             lastBorrowTime = System.currentTimeMillis();
+            lastUseTime = lastBorrowTime;
             if (logAbandoned) {
-                borrowedBy = new AbandonedObjectException();
+                borrowedBy = new AbandonedObjectCreatedException();
             }
             return true;
         } else if (state == PooledObjectState.EVICTION) {
@@ -257,15 +260,20 @@ public class DefaultPooledObject<T> impl
         state = PooledObjectState.INVALID;
     }
 
-    /**
-     * Prints the stack trace of the code that borrowed this pooled object to
-     * the supplied writer.
-     */
+    @Override
+    public void use() {
+        lastUseTime = System.currentTimeMillis();
+        usedBy = new Exception("The last code to use this object was:");
+    }
+
     @Override
     public void printStackTrace(PrintWriter writer) {
         if (borrowedBy != null) {
             borrowedBy.printStackTrace(writer);
         }
+        if (usedBy != null) {
+            usedBy.printStackTrace(writer);
+        }
     }
 
     /**
@@ -298,7 +306,7 @@ public class DefaultPooledObject<T> impl
         this.logAbandoned = logAbandoned;
     }
 
-    static class AbandonedObjectException extends Exception {
+    static class AbandonedObjectCreatedException extends Exception {
 
         private static final long serialVersionUID = 7398692158058772916L;
 
@@ -310,7 +318,7 @@ public class DefaultPooledObject<T> impl
 
         private final long _createdTime;
 
-        public AbandonedObjectException() {
+        public AbandonedObjectCreatedException() {
             _createdTime = System.currentTimeMillis();
         }
 

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java?rev=1531310&r1=1531309&r2=1531310&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
Fri Oct 11 14:31:05 2013
@@ -894,7 +894,7 @@ public class GenericObjectPool<T> extend
             PooledObject<T> pooledObject = it.next();
             synchronized (pooledObject) {
                 if (pooledObject.getState() == PooledObjectState.ALLOCATED &&
-                        pooledObject.getLastUsed() <= timeout) {
+                        pooledObject.getLastUsedTime() <= timeout) {
                     pooledObject.markAbandoned();
                     remove.add(pooledObject);
                 }
@@ -921,7 +921,11 @@ public class GenericObjectPool<T> extend
 
     @Override
     public void use(T pooledObject) {
-        // TODO Auto-generated method stub
+        AbandonedConfig ac = this.abandonedConfig;
+        if (ac != null && ac.getUseUsageTracking()) {
+            PooledObject<T> wrapper = allObjects.get(pooledObject);
+            wrapper.use();
+        }
     }
 
 



Mime
View raw message