@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())); }
@Override public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) { boolean wasEnabled = getPoolProperties().isPoolSweeperEnabled(); getPoolProperties().setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); boolean shouldBeEnabled = getPoolProperties().isPoolSweeperEnabled(); // make sure pool cleaner starts/stops when it should if (!wasEnabled && shouldBeEnabled) pool.initializePoolCleaner(getPoolProperties()); else if (wasEnabled && !shouldBeEnabled) pool.terminatePoolCleaner(); }
@Override public void setRemoveAbandonedTimeout(int removeAbandonedTimeout) { boolean wasEnabled = getPoolProperties().isPoolSweeperEnabled(); getPoolProperties().setRemoveAbandonedTimeout(removeAbandonedTimeout); boolean shouldBeEnabled = getPoolProperties().isPoolSweeperEnabled(); // make sure pool cleaner starts/stops when it should if (!wasEnabled && shouldBeEnabled) pool.initializePoolCleaner(getPoolProperties()); else if (wasEnabled && !shouldBeEnabled) pool.terminatePoolCleaner(); }
@Override public void setTestWhileIdle(boolean testWhileIdle) { boolean wasEnabled = getPoolProperties().isPoolSweeperEnabled(); getPoolProperties().setTestWhileIdle(testWhileIdle); boolean shouldBeEnabled = getPoolProperties().isPoolSweeperEnabled(); // make sure pool cleaner starts/stops when it should if (!wasEnabled && shouldBeEnabled) pool.initializePoolCleaner(getPoolProperties()); else if (wasEnabled && !shouldBeEnabled) pool.terminatePoolCleaner(); }
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 test2PoolCleaners() throws Exception { datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(2000); datasource.getPoolProperties().setTestWhileIdle(true); DataSource ds2 = new DataSource(datasource.getPoolProperties()); Assert.assertEquals( "Pool cleaner should not be started yet.", 0, ConnectionPool.getPoolCleaners().size()); Assert.assertNull("Pool timer should be null", ConnectionPool.getPoolTimer()); Assert.assertEquals( "Pool cleaner threads should not be present.", 0, countPoolCleanerThreads()); datasource.getConnection().close(); ds2.getConnection().close(); Assert.assertEquals( "Pool cleaner should have 2 cleaner.", 2, ConnectionPool.getPoolCleaners().size()); Assert.assertNotNull("Pool timer should not be null", ConnectionPool.getPoolTimer()); Assert.assertEquals("Pool cleaner threads should be 1.", 1, countPoolCleanerThreads()); datasource.close(); Assert.assertEquals( "Pool cleaner should have 1 cleaner.", 1, ConnectionPool.getPoolCleaners().size()); Assert.assertNotNull("Pool timer should not be null", ConnectionPool.getPoolTimer()); ds2.close(); Assert.assertEquals( "Pool shutdown, no cleaners should be present.", 0, ConnectionPool.getPoolCleaners().size()); Assert.assertNull("Pool timer should be null after shutdown", ConnectionPool.getPoolTimer()); Assert.assertEquals( "Pool cleaner threads should not be present after close.", 0, countPoolCleanerThreads()); }
@Override public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) { boolean wasEnabled = getPoolProperties().isPoolSweeperEnabled(); getPoolProperties().setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); boolean shouldBeEnabled = getPoolProperties().isPoolSweeperEnabled(); // make sure pool cleaner starts/stops when it should if (!wasEnabled && shouldBeEnabled) { pool.initializePoolCleaner(getPoolProperties()); } else if (wasEnabled) { pool.terminatePoolCleaner(); if (shouldBeEnabled) { pool.initializePoolCleaner(getPoolProperties()); } } }
/** {@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 int getActive() { return pool.getActive(); }
/** {@inheritDoc} */ @Override public void poolClosed(ConnectionPool pool) { perPoolStats.remove(pool.getName()); super.poolClosed(pool); }
@Override public void reset(ConnectionPool parent, PooledConnection con) { super.reset(parent, con); if (parent != null) queries = SlowQueryReport.perPoolStats.get(parent.getName()); else queries = null; }
public PoolConfiguration getPoolProperties() { return pool.getPoolProperties(); }
@Override public void testIdle() { pool.testAllIdle(); }
@Override public void checkAbandoned() { pool.checkAbandoned(); }
// ================================================================= // POOL OPERATIONS // ================================================================= @Override public void checkIdle() { pool.checkIdle(); }
/** {@inheritDoc} */ @Override public void purge() { pool.purge(); }
@Override public int getIdle() { return pool.getIdle(); }
@Override public int getSize() { return pool.getSize(); }
/** {@inheritDoc} */ @Override public void purgeOnReturn() { pool.purgeOnReturn(); }
@Override public int getWaitCount() { return pool.getWaitCount(); }