Search in sources :

Example 6 with ConnectionPool

use of org.apache.tomcat.jdbc.pool.ConnectionPool in project tomcat by apache.

the class Bug53367 method testPool.

@Test
public void testPool() throws SQLException, InterruptedException {
    DriverManager.setLoginTimeout(1);
    PoolProperties poolProperties = new DefaultProperties();
    int threadsCount = 3;
    poolProperties.setMaxActive(threadsCount);
    poolProperties.setMaxIdle(threadsCount);
    poolProperties.setMinIdle(0);
    poolProperties.setMaxWait(5000);
    poolProperties.setInitialSize(0);
    poolProperties.setRemoveAbandoned(true);
    poolProperties.setRemoveAbandonedTimeout(300);
    poolProperties.setRollbackOnReturn(true);
    poolProperties.setFairQueue(fairQueue);
    final DataSource ds = new DataSource(poolProperties);
    final CountDownLatch openedLatch = new CountDownLatch(threadsCount);
    final CountDownLatch closedLatch = new CountDownLatch(threadsCount);
    final CountDownLatch toCloseLatch = new CountDownLatch(1);
    for (int i = 0; i < threadsCount; i++) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Connection connection = ds.getConnection();
                    openedLatch.countDown();
                    toCloseLatch.await();
                    connection.close();
                    closedLatch.countDown();
                } catch (Exception e) {
                    System.err.println("Step 1:" + e.getMessage());
                }
            }
        }).start();
    }
    openedLatch.await();
    ConnectionPool pool = ds.getPool();
    //Now we have 3 initialized busy connections
    Assert.assertEquals(0, pool.getIdle());
    Assert.assertEquals(threadsCount, pool.getActive());
    Assert.assertEquals(threadsCount, pool.getSize());
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < threadsCount; i++) {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    ds.getConnection();
                } catch (Exception e) {
                    System.err.println("Step 2:" + e.getMessage());
                }
            }
        });
        thread.start();
        threads.add(thread);
    }
    for (Thread thread : threads) {
        thread.interrupt();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    //Still 3 active connections
    Assert.assertEquals(0, pool.getIdle());
    Assert.assertEquals(threadsCount, pool.getActive());
    Assert.assertEquals(threadsCount, pool.getSize());
    toCloseLatch.countDown();
    closedLatch.await();
    //Here comes the bug! No more active connections and unable to establish new connections.
    // <-- Should be threadsCount (3) here
    Assert.assertEquals(threadsCount, pool.getIdle());
    Assert.assertEquals(0, pool.getActive());
    Assert.assertEquals(threadsCount, pool.getSize());
    final AtomicInteger failedCount = new AtomicInteger();
    final ArrayBlockingQueue<Connection> cons = new ArrayBlockingQueue<>(threadsCount);
    threads.clear();
    for (int i = 0; i < threadsCount; i++) {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    cons.add(ds.getConnection());
                } catch (PoolExhaustedException e) {
                    failedCount.incrementAndGet();
                    System.err.println("Step 3:" + e.getMessage());
                } catch (Exception e) {
                    System.err.println("Step 4:" + e.getMessage());
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();
        threads.add(thread);
    }
    for (Thread thread : threads) {
        thread.join();
    }
    Assert.assertEquals(0, failedCount.get());
    Assert.assertEquals(0, pool.getIdle());
    Assert.assertEquals(threadsCount, pool.getActive());
    Assert.assertEquals(threadsCount, pool.getSize());
    for (Connection con : cons) {
        con.close();
    }
    Assert.assertEquals(threadsCount, pool.getIdle());
    Assert.assertEquals(0, pool.getActive());
    Assert.assertEquals(threadsCount, pool.getSize());
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) DefaultProperties(org.apache.tomcat.jdbc.test.DefaultProperties) PoolExhaustedException(org.apache.tomcat.jdbc.pool.PoolExhaustedException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) SQLException(java.sql.SQLException) PoolExhaustedException(org.apache.tomcat.jdbc.pool.PoolExhaustedException) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 7 with ConnectionPool

use of org.apache.tomcat.jdbc.pool.ConnectionPool in project tomcat by apache.

the class TestSlowQueryReport method testSlowSql.

@Test
public void testSlowSql() throws Exception {
    int count = 3;
    this.datasource.setMaxActive(1);
    this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName() + "(threshold=50)");
    Connection con = this.datasource.getConnection();
    for (int i = 0; i < count; i++) {
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(superSlowSql);
        rs.close();
        st.close();
    }
    Map<String, SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
    Assert.assertNotNull(map);
    Assert.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(superSlowSql);
        ResultSet rs = st.executeQuery();
        rs.close();
        st.close();
    }
    System.out.println("Stats:" + stats);
    for (int i = 0; i < count; i++) {
        CallableStatement st = con.prepareCall(superSlowSql);
        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
    Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) SlowQueryReport(org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 8 with ConnectionPool

use of org.apache.tomcat.jdbc.pool.ConnectionPool in project tomcat by apache.

the class TestSlowQueryReport method testFailedSql.

@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()));
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) SlowQueryReport(org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 9 with ConnectionPool

use of org.apache.tomcat.jdbc.pool.ConnectionPool in project tomcat by apache.

the class TestSlowQueryReport method testFastSql.

@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(1, map.size());
    ConnectionPool pool = datasource.getPool();
    con.close();
    tearDown();
    Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 10 with ConnectionPool

use of org.apache.tomcat.jdbc.pool.ConnectionPool in project platformlayer by platformlayer.

the class TomcatJdbcPoolMetricsReporter method addMetrics.

@Override
public void addMetrics(MetricTreeObject tree) {
    ConnectionPool pool = getPool();
    if (pool == null) {
        // TODO: Should we handle this properly??
        return;
    }
    MetricTreeObject subtree = tree.getSubtree(key);
    // Statistics stats = pool.getStatistics();
    // subtree.addInt("hitCount", stats.getCacheHits());
    // subtree.addInt("missCount", stats.getCacheMiss());
    // subtree.addInt("connectionsRequested", stats.getConnectionsRequested());
    // subtree.addInt("statementsCached", stats.getStatementsCached());
    // subtree.addInt("statementsExecuted", stats.getStatementsExecuted());
    // subtree.addInt("statementsPrepared", stats.getStatementsPrepared());
    // subtree.addInt("cumulativeConnectionWaitTime", stats.getCumulativeConnectionWaitTime());
    // subtree.addInt("cumulativeStatementExecutionTime", stats.getCumulativeStatementExecutionTime());
    // subtree.addInt("cumulativeStatementPrepareTime", stats.getCumulativeStatementPrepareTime());
    //
    subtree.addInt("activeConnections", pool.getActive());
    subtree.addInt("idleConnections", pool.getIdle());
    subtree.addInt("waitCount", pool.getWaitCount());
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool)

Aggregations

ConnectionPool (org.apache.tomcat.jdbc.pool.ConnectionPool)11 Connection (java.sql.Connection)6 Test (org.junit.Test)6 CallableStatement (java.sql.CallableStatement)5 Statement (java.sql.Statement)5 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 SlowQueryReport (org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport)3 ArrayList (java.util.ArrayList)2 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)2 DefaultProperties (org.apache.tomcat.jdbc.test.DefaultProperties)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 Hashtable (java.util.Hashtable)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 DataSource (org.apache.tomcat.jdbc.pool.DataSource)1