this.maxQueries = maxQueries;
}
+
+
+ @Override
+ protected String reportFailedQuery(String query, Object[] args, String name, long start, Throwable t) {
+ String sql = super.reportFailedQuery(query, args, name, start, t);
+ if (this.maxQueries > 0 ) {
+ long now = System.currentTimeMillis();
+ long delta = now - start;
+ QueryStats qs = this.getQueryStats(sql);
+ qs.failure(delta, now);
+ }
+ return sql;
+ }
+
+ @Override
+ protected String reportSlowQuery(String query, Object[] args, String name, long start, long delta) {
+ String sql = super.reportSlowQuery(query, args, name, start, delta);
+ if (this.maxQueries > 0 ) {
+ QueryStats qs = this.getQueryStats(sql);
+ qs.add(delta, start);
+ }
+ return sql;
+ }
+
/**
* invoked when the connection receives the close request
* Not used for now.
import java.util.Map;
import org.apache.tomcat.jdbc.pool.ConnectionPool;
-import org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx;
+import org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport;
public class TestSlowQueryReport extends DefaultTestCase {
int count = 3;
this.init();
this.datasource.setMaxActive(1);
- this.datasource.setJdbcInterceptors(SlowQueryReportJmx.class.getName());
+ this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName()+"(threshold=50)");
Connection con = this.datasource.getConnection();
- String slowSql = "select count(1) from test where val1 like 'ewqeq' and val2 = 'ewrre' and val3 = 'sdada' and val4 = 'dadada'";
+ String slowSql = "select count(1) from test where val1 like 'ewq%eq' and val2 = 'ew%rre' and val3 = 'sda%da' and val4 = 'dad%ada'";
for (int i=0; i<count; i++) {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(slowSql);
rs.close();
st.close();
}
- Map<String,SlowQueryReportJmx.QueryStats> map = SlowQueryReportJmx.getPoolStats(datasource.getPool().getName());
+ Map<String,SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
assertNotNull(map);
assertEquals(1,map.size());
String key = map.keySet().iterator().next();
- SlowQueryReportJmx.QueryStats stats = map.get(key);
+ SlowQueryReport.QueryStats stats = map.get(key);
System.out.println("Stats:"+stats);
for (int i=0; i<count; i++) {
con.close();
tearDown();
//make sure we actually did clean up when the pool closed
- assertNull(SlowQueryReportJmx.getPoolStats(pool.getName()));
+ assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
public void testFastSql() throws Exception {
int count = 3;
this.init();
this.datasource.setMaxActive(1);
- this.datasource.setJdbcInterceptors(SlowQueryReportJmx.class.getName());
+ this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName());
Connection con = this.datasource.getConnection();
String slowSql = "select 1";
for (int i=0; i<count; i++) {
rs.close();
st.close();
}
- Map<String,SlowQueryReportJmx.QueryStats> map = SlowQueryReportJmx.getPoolStats(datasource.getPool().getName());
+ Map<String,SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
assertNotNull(map);
assertEquals(0,map.size());
ConnectionPool pool = datasource.getPool();
con.close();
tearDown();
- assertNull(SlowQueryReportJmx.getPoolStats(pool.getName()));
+ assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
public void testFailedSql() throws Exception {
int count = 3;
this.init();
this.datasource.setMaxActive(1);
- this.datasource.setJdbcInterceptors(SlowQueryReportJmx.class.getName());
+ this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName());
Connection con = this.datasource.getConnection();
String slowSql = "select 1 from non_existent";
int exceptionCount = 0;
st.close();
}
- Map<String,SlowQueryReportJmx.QueryStats> map = SlowQueryReportJmx.getPoolStats(datasource.getPool().getName());
+ 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();
- SlowQueryReportJmx.QueryStats stats = map.get(key);
+ SlowQueryReport.QueryStats stats = map.get(key);
System.out.println("Stats:"+stats);
con.close();
tearDown();
- assertNull(SlowQueryReportJmx.getPoolStats(pool.getName()));
+ assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}