You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2012/04/23 16:00:59 UTC

svn commit: r1329245 - in /commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl: GenericKeyedObjectPool.java GenericKeyedObjectPoolMBean.java GenericObjectPool.java GenericObjectPoolMBean.java

Author: markt
Date: Mon Apr 23 14:00:59 2012
New Revision: 1329245

URL: http://svn.apache.org/viewvc?rev=1329245&view=rev
Log:
Make the stack trace for pool creation available via JMX since registering pools via JMX may lead to a memory leak if the pool is not closed. The creation stack trace will assist in locating the source of the leak. This is currently triggering a unit test failure which was the initial motivation for adding this feature.

Modified:
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java?rev=1329245&r1=1329244&r2=1329245&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java Mon Apr 23 14:00:59 2012
@@ -302,6 +302,9 @@ public class GenericKeyedObjectPool<K,T>
         for (int i = 0; i < SWALLOWED_EXCEPTION_QUEUE_SIZE; i++) {
             swallowedExceptions.add(null);
         }
+        
+        // Populate the creation stack trace
+        this.creationStackTrace = getStackTrace(new Exception());
     }
 
     //--- configuration methods --------------------------------------
@@ -1061,14 +1064,8 @@ public class GenericKeyedObjectPool<K,T>
      }
 
      
-    private void swallowException (Exception e) {
-        // Need the exception in string form to prevent the retention of
-        // references to classes in the stack trace that could trigger a memory
-        // leak in a container environment
-        Writer w = new StringWriter();
-        PrintWriter pw = new PrintWriter(w);
-        e.printStackTrace(pw);
-        String msg = w.toString();
+    private void swallowException(Exception e) {
+        String msg = getStackTrace(e);
 
         if (oname != null) {
             Notification n = new Notification(NOTIFICATION_SWALLOWED_EXCEPTION,
@@ -1083,6 +1080,15 @@ public class GenericKeyedObjectPool<K,T>
         }
     }
 
+    private String getStackTrace(Exception e) {
+        // Need the exception in string form to prevent the retention of
+        // references to classes in the stack trace that could trigger a memory
+        // leak in a container environment
+        Writer w = new StringWriter();
+        PrintWriter pw = new PrintWriter(w);
+        e.printStackTrace(pw);
+        return w.toString();
+    }
          
      private void updateStatsReturn(long activeTime) {
          returnedCount.incrementAndGet();
@@ -2133,6 +2139,11 @@ public class GenericKeyedObjectPool<K,T>
         return oname;
     }
 
+    @Override
+    public String getCreationStackTrace() {
+        return creationStackTrace;
+    }
+    
     //--- inner classes ----------------------------------------------
 
     /**
@@ -2499,7 +2510,8 @@ public class GenericKeyedObjectPool<K,T>
             
     private final ObjectName oname;
     private final NotificationBroadcasterSupport jmxNotificationSupport;
-
+    private final String creationStackTrace;
+    
     private static final String ONAME_BASE =
         "org.apache.commoms.pool2:type=GenericKeyedObjectPool,name=";
 }

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java?rev=1329245&r1=1329244&r2=1329245&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java Mon Apr 23 14:00:59 2012
@@ -49,4 +49,5 @@ public interface GenericKeyedObjectPoolM
     long getMeanBorrowWaitTimeMillis();
     long getMaxBorrowWaitTimeMillis();
     String[] getSwallowedExceptions();
+    String getCreationStackTrace();
 }

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=1329245&r1=1329244&r2=1329245&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 Mon Apr 23 14:00:59 2012
@@ -256,6 +256,9 @@ public class GenericObjectPool<T> extend
         for (int i = 0; i < SWALLOWED_EXCEPTION_QUEUE_SIZE; i++) {
             swallowedExceptions.add(null);
         }
+        
+        // Populate the creation stack trace
+        this.creationStackTrace = getStackTrace(new Exception());
     }
 
 
@@ -989,15 +992,9 @@ public class GenericObjectPool<T> extend
         }
     }
 
-    private void swallowException (Exception e) {
-        // Need the exception in string form to prevent the retention of
-        // references to classes in the stack trace that could trigger a memory
-        // leak in a container environment
-        Writer w = new StringWriter();
-        PrintWriter pw = new PrintWriter(w);
-        e.printStackTrace(pw);
-        String msg = w.toString();
-
+    private void swallowException(Exception e) {
+        String msg = getStackTrace(e);
+        
         if (oname != null) {
             Notification n = new Notification(NOTIFICATION_SWALLOWED_EXCEPTION,
                     oname, swallowedExcpetionCount.incrementAndGet(), msg);
@@ -1011,6 +1008,16 @@ public class GenericObjectPool<T> extend
         }
     }
 
+    private String getStackTrace(Exception e) {
+        // Need the exception in string form to prevent the retention of
+        // references to classes in the stack trace that could trigger a memory
+        // leak in a container environment
+        Writer w = new StringWriter();
+        PrintWriter pw = new PrintWriter(w);
+        e.printStackTrace(pw);
+        return w.toString();
+    }
+
     /**
      * {@inheritDoc}
      * <p>
@@ -1525,6 +1532,11 @@ public class GenericObjectPool<T> extend
         return oname;
     }
     
+    @Override
+    public String getCreationStackTrace() {
+        return creationStackTrace;
+    }
+
     // --- inner classes ----------------------------------------------
 
     /**
@@ -1791,6 +1803,7 @@ public class GenericObjectPool<T> extend
     
     private final ObjectName oname;
     private final NotificationBroadcasterSupport jmxNotificationSupport;
+    private final String creationStackTrace;
 
     private static final String ONAME_BASE =
         "org.apache.commoms.pool2:type=GenericObjectPool,name=";

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java?rev=1329245&r1=1329244&r2=1329245&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java Mon Apr 23 14:00:59 2012
@@ -45,4 +45,5 @@ public interface GenericObjectPoolMBean 
     long getMeanBorrowWaitTimeMillis();
     long getMaxBorrowWaitTimeMillis();
     String[] getSwallowedExceptions();
+    String getCreationStackTrace();
 }