Search in sources :

Example 1 with DefaultProperties

use of org.apache.tomcat.jdbc.test.DefaultProperties 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)

Example 2 with DefaultProperties

use of org.apache.tomcat.jdbc.test.DefaultProperties 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 3 with DefaultProperties

use of org.apache.tomcat.jdbc.test.DefaultProperties in project tomcat by apache.

the class Bug54225 method testPool.

@Test
public void testPool() throws SQLException {
    PoolProperties poolProperties = new DefaultProperties();
    poolProperties.setMinIdle(0);
    poolProperties.setInitialSize(0);
    poolProperties.setMaxWait(5000);
    poolProperties.setRemoveAbandoned(true);
    poolProperties.setRemoveAbandonedTimeout(300);
    poolProperties.setRollbackOnReturn(true);
    poolProperties.setInitSQL(initSQL);
    final DataSource ds = new DataSource(poolProperties);
    ds.getConnection().close();
    assertNull(poolProperties.getInitSQL());
}
Also used : DefaultProperties(org.apache.tomcat.jdbc.test.DefaultProperties) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource) Test(org.junit.Test)

Example 4 with DefaultProperties

use of org.apache.tomcat.jdbc.test.DefaultProperties in project tomcat by apache.

the class Bug54227 method testPool.

@Test
public void testPool() throws SQLException, InterruptedException {
    PoolProperties poolProperties = new DefaultProperties();
    poolProperties.setMinIdle(0);
    poolProperties.setInitialSize(0);
    poolProperties.setMaxActive(1);
    poolProperties.setMaxWait(5000);
    poolProperties.setMaxAge(100);
    poolProperties.setRemoveAbandoned(false);
    final DataSource ds = new DataSource(poolProperties);
    Connection con;
    Connection actual1;
    Connection actual2;
    con = ds.getConnection();
    actual1 = ((PooledConnection) con).getConnection();
    con.close();
    con = ds.getConnection();
    actual2 = ((PooledConnection) con).getConnection();
    assertSame(actual1, actual2);
    con.close();
    Thread.sleep(150);
    con = ds.getConnection();
    actual2 = ((PooledConnection) con).getConnection();
    assertNotSame(actual1, actual2);
    con.close();
}
Also used : DefaultProperties(org.apache.tomcat.jdbc.test.DefaultProperties) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource) Test(org.junit.Test)

Example 5 with DefaultProperties

use of org.apache.tomcat.jdbc.test.DefaultProperties in project tomcat by apache.

the class Bug54978 method testIllegalValidationQueryWithLegalInit.

@Test
public void testIllegalValidationQueryWithLegalInit() throws SQLException {
    PoolProperties poolProperties = new DefaultProperties();
    poolProperties.setMinIdle(0);
    poolProperties.setInitialSize(1);
    poolProperties.setMaxActive(1);
    poolProperties.setMaxWait(5000);
    poolProperties.setMaxAge(100);
    poolProperties.setRemoveAbandoned(false);
    poolProperties.setTestOnBorrow(true);
    poolProperties.setTestOnConnect(false);
    poolProperties.setValidationQuery("sdadsada");
    poolProperties.setInitSQL("SELECT 1");
    final DataSource ds = new DataSource(poolProperties);
    ds.getConnection().close();
}
Also used : DefaultProperties(org.apache.tomcat.jdbc.test.DefaultProperties) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource) Test(org.junit.Test)

Aggregations

DefaultProperties (org.apache.tomcat.jdbc.test.DefaultProperties)6 DataSource (org.apache.tomcat.jdbc.pool.DataSource)5 PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)5 Test (org.junit.Test)5 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)2 ConnectionPool (org.apache.tomcat.jdbc.pool.ConnectionPool)2 CallableStatement (java.sql.CallableStatement)1 Statement (java.sql.Statement)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 PooledConnection (javax.sql.PooledConnection)1 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)1 PoolExhaustedException (org.apache.tomcat.jdbc.pool.PoolExhaustedException)1