Search in sources :

Example 1 with ConnectionPool

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

the class TestSlowQueryReport method testSlowSqlJmx.

@Test
public void testSlowSqlJmx() throws Exception {
    int count = 1;
    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);
    ClientListener listener = new ClientListener();
    ConnectionPool pool = datasource.getPool();
    ManagementFactory.getPlatformMBeanServer().addNotificationListener(new SlowQueryReportJmx().getObjectName(SlowQueryReportJmx.class, pool.getName()), listener, null, null);
    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);
    Assert.assertEquals("Expecting to have received " + (2 * count) + " notifications.", 2 * count, listener.notificationCount.get());
    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) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SlowQueryReportJmx(org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx) SlowQueryReport(org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 2 with ConnectionPool

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

the class JmxPasswordTest method setUp.

@Before
public void setUp() throws Exception {
    this.datasource.setDriverClassName(Driver.class.getName());
    this.datasource.setUrl("jdbc:tomcat:test");
    this.datasource.setPassword(password);
    this.datasource.setUsername(username);
    this.datasource.getConnection().close();
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    String domain = "tomcat.jdbc";
    Hashtable<String, String> properties = new Hashtable<>();
    properties.put("type", "ConnectionPool");
    properties.put("class", this.getClass().getName());
    oname = new ObjectName(domain, properties);
    ConnectionPool pool = datasource.createPool();
    org.apache.tomcat.jdbc.pool.jmx.ConnectionPool jmxPool = new org.apache.tomcat.jdbc.pool.jmx.ConnectionPool(pool);
    mbs.registerMBean(jmxPool, oname);
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) Hashtable(java.util.Hashtable) Driver(org.apache.tomcat.jdbc.test.driver.Driver) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer) Before(org.junit.Before)

Example 3 with ConnectionPool

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

the class ManagedPooledDataSource method start.

@Override
public void start() throws Exception {
    final ConnectionPool connectionPool = createPool();
    metricRegistry.register(name(getClass(), connectionPool.getName(), "active"), (Gauge<Integer>) connectionPool::getActive);
    metricRegistry.register(name(getClass(), connectionPool.getName(), "idle"), (Gauge<Integer>) connectionPool::getIdle);
    metricRegistry.register(name(getClass(), connectionPool.getName(), "waiting"), (Gauge<Integer>) connectionPool::getWaitCount);
    metricRegistry.register(name(getClass(), connectionPool.getName(), "size"), (Gauge<Integer>) connectionPool::getSize);
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool)

Example 4 with ConnectionPool

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

the class CloseableLiquibaseTest method testWhenClosingAllConnectionsInPoolIsReleased.

@Test
public void testWhenClosingAllConnectionsInPoolIsReleased() throws Exception {
    ConnectionPool pool = dataSource.getPool();
    assertThat(pool.getActive()).isEqualTo(1);
    liquibase.close();
    assertThat(pool.getActive()).isZero();
    assertThat(pool.getIdle()).isZero();
    assertThat(pool.isClosed()).isTrue();
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) Test(org.junit.Test)

Example 5 with ConnectionPool

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

the class Bug51582 method main.

public static void main(String[] args) throws SQLException {
    org.apache.tomcat.jdbc.pool.DataSource datasource = null;
    PoolConfiguration p = new DefaultProperties();
    p.setJmxEnabled(true);
    p.setTestOnBorrow(false);
    p.setTestOnReturn(false);
    p.setValidationInterval(1000);
    p.setTimeBetweenEvictionRunsMillis(2000);
    p.setMaxWait(2000);
    p.setMinEvictableIdleTimeMillis(1000);
    datasource = new org.apache.tomcat.jdbc.pool.DataSource();
    datasource.setPoolProperties(p);
    datasource.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx(threshold=200)");
    ConnectionPool pool = datasource.createPool();
    Connection con = pool.getConnection();
    Statement st = con.createStatement();
    try {
        st.execute("DROP ALIAS SLEEP");
    } catch (Exception ignore) {
    // Ignore
    }
    st.execute("CREATE ALIAS SLEEP AS $$\nboolean sleep() {\n        try {\n            Thread.sleep(10000);\n            return true;        } catch (Exception x) {\n            return false;\n        }\n}\n$$;");
    st.close();
    con.close();
    int iter = 0;
    while ((iter++) < 10) {
        final Connection connection = pool.getConnection();
        final CallableStatement s = connection.prepareCall("{CALL SLEEP()}");
        List<Thread> threadList = new ArrayList<>();
        for (int l = 0; l < 3; l++) {
            final int i = l;
            Thread thread = new Thread() {

                @Override
                public void run() {
                    try {
                        if (i == 0) {
                            Thread.sleep(1000);
                            s.cancel();
                        } else if (i == 1) {
                            // or use some other statement which will block
                            // for a longer time
                            long start = System.currentTimeMillis();
                            System.out.println("[" + getName() + "] Calling SP SLEEP");
                            s.execute();
                            System.out.println("[" + getName() + "] Executed SP SLEEP [" + (System.currentTimeMillis() - start) + "]");
                        } else {
                            Thread.sleep(1000);
                            connection.close();
                        }
                    } catch (InterruptedException e) {
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            };
            threadList.add(thread);
            thread.start();
        }
        for (Thread t : threadList) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) DefaultProperties(org.apache.tomcat.jdbc.test.DefaultProperties) SQLException(java.sql.SQLException) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement)

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