Search in sources :

Example 11 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project dal by ctripcorp.

the class DataSourceValidator method validate.

@Override
public boolean validate(Connection connection, int validateAction) {
    boolean isValid = false;
    try {
        String query = null;
        int validationQueryTimeout = -1;
        if (validateAction == PooledConnection.VALIDATE_INIT) {
            PoolProperties poolProperties = getPoolProperties(connection);
            if (poolProperties != null) {
                query = poolProperties.getInitSQL();
                validationQueryTimeout = poolProperties.getValidationQueryTimeout();
                if (validationQueryTimeout <= 0) {
                    validationQueryTimeout = DEFAULT_VALIDATE_TIMEOUT_IN_SECONDS;
                }
            }
        }
        if (query == null) {
            if (connection instanceof MySQLConnection) {
                MySQLConnection mySqlConnection = (MySQLConnection) connection;
                isValid = MySqlConnectionHelper.isValid(mySqlConnection, DEFAULT_VALIDATE_TIMEOUT_IN_SECONDS);
            } else {
                isValid = connection.isValid(DEFAULT_VALIDATE_TIMEOUT_IN_SECONDS);
            }
            if (!isValid) {
                LOGGER.warn("isValid() returned false.");
            }
        } else {
            Statement stmt = null;
            try {
                stmt = connection.createStatement();
                stmt.setQueryTimeout(validationQueryTimeout);
                stmt.execute(query);
                isValid = true;
            } finally {
                if (stmt != null)
                    try {
                        stmt.close();
                    } catch (Exception ignore2) {
                    /* NOOP */
                    }
            }
        }
    } catch (Throwable ex) {
        LOGGER.warn("Datasource validation error", ex);
    }
    return isValid;
}
Also used : Statement(java.sql.Statement) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) MySQLConnection(com.mysql.jdbc.MySQLConnection)

Example 12 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project dal by ctripcorp.

the class DataSourceUtil method createDataSource.

private static DataSource createDataSource(String url, String userName, String password, String driverClass) throws Exception {
    PoolProperties p = new PoolProperties();
    p.setUrl(url);
    p.setUsername(userName);
    p.setPassword(password);
    p.setDriverClassName(driverClass);
    p.setJmxEnabled(false);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setTestOnReturn(false);
    p.setValidationQuery("SELECT 1");
    p.setValidationQueryTimeout(5);
    p.setValidationInterval(30000L);
    p.setTimeBetweenEvictionRunsMillis(5000);
    p.setMaxActive(100);
    p.setMinIdle(0);
    p.setMaxWait(10000);
    p.setMaxAge(0L);
    p.setInitialSize(1);
    p.setRemoveAbandonedTimeout(60);
    p.setRemoveAbandoned(true);
    p.setLogAbandoned(true);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(p);
    ds.createPool();
    return ds;
}
Also used : PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(javax.sql.DataSource)

Example 13 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties 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 14 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties 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 15 with PoolProperties

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

Aggregations

PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)26 DataSource (org.apache.tomcat.jdbc.pool.DataSource)10 Test (org.junit.Test)10 Connection (java.sql.Connection)6 DefaultProperties (org.apache.tomcat.jdbc.test.DefaultProperties)5 Properties (java.util.Properties)4 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)4 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 DataSource (javax.sql.DataSource)3 InterceptorDefinition (org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition)3 TrapException (org.apache.tomcat.jdbc.pool.TrapException)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ConnectionPool (org.apache.tomcat.jdbc.pool.ConnectionPool)2 InterceptorProperty (org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorProperty)2