refactor the slow query report a bit
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 10 Dec 2008 23:38:13 +0000 (23:38 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 10 Dec 2008 23:38:13 +0000 (23:38 +0000)
also add the ability to remove a listener

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@725487 13f79535-47bb-0310-9956-ffa450edef68

modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/SlowQueryReport.java

index 57caa81..00c9a57 100644 (file)
@@ -263,6 +263,10 @@ public class ConnectionPool {
         listeners.add(listener);
     }
     
+    public void removeCloseListener(CloseListener listener) {
+        listeners.remove(listener);
+    }
+    
     /**
      * Closes the pool and all disconnects all idle connections
      * Active connections will be closed upon the {@link java.sql.Connection#close close} method is called
index 0487a34..e3e5045 100644 (file)
@@ -29,6 +29,8 @@ import java.util.IdentityHashMap;
 import java.util.LinkedHashMap;
 import java.util.Map.Entry;
 
+import javax.management.openmbean.CompositeData;
+
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.jdbc.pool.ConnectionPool;
@@ -110,7 +112,7 @@ public class SlowQueryReport extends AbstractCreateStatementInterceptor implemen
      */
     @Override
     public void closeInvoked() {
-        // TODO Auto-generated method stub
+        queries = null;
         
     }
     
@@ -187,6 +189,10 @@ public class SlowQueryReport extends AbstractCreateStatementInterceptor implemen
         }
         this.pool = parent;
     }
+    
+    public void finalize() {
+        if (pool!=null) pool.removeCloseListener(this);
+    }
 
     
     public void poolClosed(ConnectionPool pool) {
@@ -194,6 +200,10 @@ public class SlowQueryReport extends AbstractCreateStatementInterceptor implemen
         perPoolStats.remove(pool);
     }
     
+    public CompositeData[] getSlowQueriesCD() {
+        return null;
+    }
+    
 
     
     /**
@@ -326,29 +336,32 @@ public class SlowQueryReport extends AbstractCreateStatementInterceptor implemen
             long delta = (process)?(System.currentTimeMillis()-start):Long.MIN_VALUE;
             //see if we meet the requirements to measure
             if (delta>threshold) {
-                //extract the query string
-                String sql = (query==null && args!=null &&  args.length>0)?(String)args[0]:query;
-                //if we do batch execution, then we name the query 'batch'
-                if (sql==null && compare(executes[3],name)) {
-                    sql = "batch";
-                }
-                //if we have a query, record the stats
-                if (sql!=null) {
-                    QueryStats qs = SlowQueryReport.this.queries.get(sql);
-                    if (qs == null) {
-                        qs = new QueryStats(sql);
-                        SlowQueryReport.this.queries.put((String)sql,qs);
-                    }
-                    qs.add(delta,start);
-                }
+                reportSlowQuery(args, name, start, delta);
             }
             //perform close cleanup
             if (close) {
                 closed=true;
                 delegate = null;
-                queries = null;
             }
             return result;
         }
+
+        protected void reportSlowQuery(Object[] args, final String name, long start, long delta) {
+            //extract the query string
+            String sql = (query==null && args!=null &&  args.length>0)?(String)args[0]:query;
+            //if we do batch execution, then we name the query 'batch'
+            if (sql==null && compare(executes[3],name)) {
+                sql = "batch";
+            }
+            //if we have a query, record the stats
+            if (sql!=null) {
+                QueryStats qs = SlowQueryReport.this.queries.get(sql);
+                if (qs == null) {
+                    qs = new QueryStats(sql);
+                    SlowQueryReport.this.queries.put((String)sql,qs);
+                }
+                qs.add(delta,start);
+            }
+        }
     }
 }