public void testSlowSqlJmx() throws Exception {
    int count = 1;
    this.init();
    this.datasource.setMaxActive(1);
    this.datasource.setJdbcInterceptors(
        SlowQueryReportJmx.class.getName() + "(threshold=50,notifyPool=false)");
    Connection con = this.datasource.getConnection();
    String slowSql = "select count(1) from test where val1 like 'ewq%eq'";
    for (int i = 0; i < count; i++) {
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery(slowSql);
      rs.close();
      st.close();
    }
    Map<String, SlowQueryReport.QueryStats> map =
        SlowQueryReport.getPoolStats(datasource.getPool().getName());
    assertNotNull(map);
    assertEquals(1, map.size());
    String key = map.keySet().iterator().next();
    SlowQueryReport.QueryStats stats = map.get(key);
    System.out.println("Stats:" + stats);
    ClientListener listener = new ClientListener();
    ConnectionPool pool = datasource.getPool();
    ManagementFactory.getPlatformMBeanServer()
        .addNotificationListener(
            SlowQueryReportJmx.getObjectName(SlowQueryReportJmx.class, pool.getName()),
            listener,
            null,
            null);

    for (int i = 0; i < count; i++) {
      PreparedStatement st = con.prepareStatement(slowSql);
      ResultSet rs = st.executeQuery();
      rs.close();
      st.close();
    }
    System.out.println("Stats:" + stats);

    for (int i = 0; i < count; i++) {
      CallableStatement st = con.prepareCall(slowSql);
      ResultSet rs = st.executeQuery();
      rs.close();
      st.close();
    }
    System.out.println("Stats:" + stats);
    assertEquals(
        "Expecting to have received " + (2 * count) + " notifications.",
        2 * count,
        listener.notificationCount);
    con.close();
    tearDown();
    // make sure we actually did clean up when the pool closed
    assertNull(SlowQueryReport.getPoolStats(pool.getName()));
  }
 @Test
 public void testFailedSql() throws Exception {
   int count = 3;
   Connection con = this.datasource.getConnection();
   for (int i = 0; i < count; i++) {
     Statement st = con.createStatement();
     try {
       ResultSet rs = st.executeQuery(failedSql);
       rs.close();
     } catch (Exception x) {
       // NO-OP
     }
     st.close();
   }
   Map<String, SlowQueryReport.QueryStats> map =
       SlowQueryReport.getPoolStats(datasource.getPool().getName());
   Assert.assertNotNull(map);
   Assert.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();
   Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
 }
 /** {@inheritDoc} */
 @Override
 public void poolStarted(ConnectionPool pool) {
   super.poolStarted(pool);
   // see if we already created a map for this pool
   queries = SlowQueryReport.perPoolStats.get(pool.getName());
   if (queries == null) {
     // create the map to hold our stats
     // however TODO we need to improve the eviction
     // selection
     queries = new ConcurrentHashMap<>();
     if (perPoolStats.putIfAbsent(pool.getName(), queries) != null) {
       // there already was one
       queries = SlowQueryReport.perPoolStats.get(pool.getName());
     }
   }
 }
  public void testSlowSql() throws Exception {
    int count = 3;
    this.init();
    this.datasource.setMaxActive(1);
    this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName() + "(threshold=50)");
    Connection con = this.datasource.getConnection();
    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, SlowQueryReport.QueryStats> map =
        SlowQueryReport.getPoolStats(datasource.getPool().getName());
    assertNotNull(map);
    assertEquals(1, map.size());
    String key = map.keySet().iterator().next();
    SlowQueryReport.QueryStats stats = map.get(key);
    System.out.println("Stats:" + stats);

    for (int i = 0; i < count; i++) {
      PreparedStatement st = con.prepareStatement(slowSql);
      ResultSet rs = st.executeQuery();
      rs.close();
      st.close();
    }
    System.out.println("Stats:" + stats);

    for (int i = 0; i < count; i++) {
      CallableStatement st = con.prepareCall(slowSql);
      ResultSet rs = st.executeQuery();
      rs.close();
      st.close();
    }
    System.out.println("Stats:" + stats);
    ConnectionPool pool = datasource.getPool();
    con.close();
    tearDown();
    // make sure we actually did clean up when the pool closed
    assertNull(SlowQueryReport.getPoolStats(pool.getName()));
  }
 @Test
 public void testFastSql() throws Exception {
   int count = 3;
   Connection con = this.datasource.getConnection();
   String fastSql = this.datasource.getValidationQuery();
   for (int i = 0; i < count; i++) {
     Statement st = con.createStatement();
     ResultSet rs = st.executeQuery(fastSql);
     rs.close();
     st.close();
   }
   Map<String, SlowQueryReport.QueryStats> map =
       SlowQueryReport.getPoolStats(datasource.getPool().getName());
   Assert.assertNotNull(map);
   Assert.assertEquals(0, map.size());
   ConnectionPool pool = datasource.getPool();
   con.close();
   tearDown();
   Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
 }
 @Override
 public void reset(ConnectionPool parent, PooledConnection con) {
   super.reset(parent, con);
   if (parent != null) queries = SlowQueryReport.perPoolStats.get(parent.getName());
   else queries = null;
 }
 /** {@inheritDoc} */
 @Override
 public void poolClosed(ConnectionPool pool) {
   perPoolStats.remove(pool.getName());
   super.poolClosed(pool);
 }