@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())); }
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())); }
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())); }