Implement startPool method to inform interceptors that pool is started
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 11 Dec 2008 22:17:02 +0000 (22:17 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 11 Dec 2008 22:17:02 +0000 (22:17 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@725838 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/DataSourceProxy.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TestSlowQueryReport.java

index 6e41ed7..5113034 100644 (file)
@@ -307,7 +307,7 @@ public class ConnectionPool {
      * @param properties PoolProperties - properties used to initialize the pool with
      * @throws SQLException
      */
-    protected void init (PoolProperties properties) throws SQLException {
+    protected void init(PoolProperties properties) throws SQLException {
         poolProperties = properties;
         //make space for 10 extra in case we flow over a bit
         busy = new ArrayBlockingQueue<PooledConnection>(properties.getMaxActive(),false);
@@ -343,6 +343,16 @@ public class ConnectionPool {
         }
 
 
+        PoolProperties.InterceptorDefinition[] proxies = getPoolProperties().getJdbcInterceptorsAsArray();
+        for (int i=0; i<proxies.length; i++) {
+            try {
+                proxies[i].getInterceptorClass().newInstance().poolStarted(this);
+            }catch (Exception x) {
+                log.warn("Unable to inform interceptor of pool start.",x);
+                close(true);
+                throw new SQLException(x);
+            }
+        }        
         //initialize the pool with its initial set of members
         PooledConnection[] initialPool = new PooledConnection[poolProperties.getInitialSize()];
         try {
index 823adeb..12c7b7c 100644 (file)
@@ -43,6 +43,7 @@ public class DataSourceProxy  {
     protected static Log log = LogFactory.getLog(DataSourceProxy.class);
     
     protected volatile ConnectionPool pool = null;
+    
     public ConnectionPool getPool() {
         return pool;
     }
@@ -173,7 +174,7 @@ public class DataSourceProxy  {
                 if (p!=null) p.close(all);
             }
         }catch (Exception x) {
-            x.printStackTrace();
+            log.warn("Error duing connection pool closure.", x);
         }
     }
 
index 176a1f3..88c6949 100644 (file)
@@ -100,4 +100,15 @@ public abstract class JdbcInterceptor implements InvocationHandler {
      */
     public void poolClosed(ConnectionPool pool) {
     }
+
+    /**
+     * This method is invoked by a connection pool when the pool is first started up, usually when the first connection is requested.
+     * Interceptor classes can override this method if they keep static
+     * variables or other tracking means around.
+     * <b>This method is only invoked on a single instance of the interceptor, and not on every instance created.</b>
+     * @param pool - the pool that is being closed.
+     */
+    public void poolStarted(ConnectionPool pool) {
+    }
+
 }
index 547ab08..82ffe15 100644 (file)
@@ -15,7 +15,6 @@ public class TestSlowQueryReport extends DefaultTestCase {
     public TestSlowQueryReport(String name) {
         super(name);
     }
-
     
     public void testSlowSql() throws Exception {
         int count = 3;
@@ -81,4 +80,36 @@ public class TestSlowQueryReport extends DefaultTestCase {
         assertNull(SlowQueryReport.getPoolStats(pool.getName()));
     }    
     
+    public void testFailedSql() throws Exception {
+        int count = 3;
+        this.init();
+        this.datasource.setMaxActive(1);
+        this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName());
+        Connection con = this.datasource.getConnection();
+        String slowSql = "select 1 from non_existent";
+        int exceptionCount = 0;
+        for (int i=0; i<count; i++) {
+            Statement st = con.createStatement();
+            try {
+                ResultSet rs = st.executeQuery(slowSql);
+                rs.close();
+            }catch (Exception x) {
+                exceptionCount++;
+            }
+            st.close();
+            
+        }
+        Map<String,SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
+        assertNotNull(map);
+        assertEquals(1,map.size());
+        ConnectionPool pool = datasource.getPool();
+        String key = map.keySet().iterator().next();
+        SlowQueryReport.QueryStats stats = map.get(key);
+        System.out.println("Stats:"+stats);
+        con.close();
+        tearDown();
+        assertNull(SlowQueryReport.getPoolStats(pool.getName()));
+    }    
+
+
 }