Search in sources :

Example 36 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class TestPoolPreparedStatement2 method test_stmtCache.

public void test_stmtCache() throws Exception {
    for (int j = 0; j < 10; ++j) {
        for (int i = 0; i < 10; ++i) {
            Connection conn = dataSource.getConnection();
            String sql = "SELECT" + i;
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.execute();
            stmt.close();
            conn.close();
        }
    }
    dataSource.setPoolPreparedStatements(true);
    for (int j = 0; j < 10; ++j) {
        for (int i = 0; i < 10; ++i) {
            Connection conn = dataSource.getConnection();
            String sql = "SELECT" + i;
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.execute();
            stmt.close();
            conn.close();
        }
    }
    for (int i = 0; i < 10 * 1; ++i) {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement("SELECT " + i);
        stmt.execute();
        stmt.close();
        conn.close();
    }
    Connection conn = dataSource.getConnection();
    DruidPooledConnection poolableConn = conn.unwrap(DruidPooledConnection.class);
    Assert.assertNotNull(poolableConn);
    Assert.assertEquals(dataSource.getMaxPoolPreparedStatementPerConnectionSize(), poolableConn.getConnectionHolder().getStatementPool().getMap().size());
    conn.close();
    Assert.assertEquals(0, dataSource.getActiveCount());
    Assert.assertEquals(1, dataSource.getPoolingCount());
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 37 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class TestRemoveAbandoned2 method test_removeAbandoned.

public void test_removeAbandoned() throws Exception {
    {
        Connection conn = dataSource.getConnection();
        conn.close();
    }
    Assert.assertEquals(0, dataSource.getActiveCount());
    Thread abandonThread = new Thread("abandoned") {

        public void run() {
            for (; ; ) {
                dataSource.removeAbandoned();
                if (Thread.interrupted()) {
                    break;
                }
            }
        }
    };
    abandonThread.start();
    for (int i = 0; i < 1000 * 100; ++i) {
        DruidPooledConnection conn = dataSource.getConnection();
        conn.close();
    }
    Assert.assertEquals(0, dataSource.getActiveCount());
    abandonThread.interrupt();
    System.out.println("removeAbandondedCount : " + dataSource.getRemoveAbandonedCount());
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Connection(java.sql.Connection)

Example 38 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class UrlNotSetTest method test_wait.

public void test_wait() throws Exception {
    Exception error = null;
    try {
        DruidPooledConnection conn = dataSource.getConnection();
        conn.close();
    } catch (SQLException ex) {
        error = ex;
    }
    assertEquals("url not set", error.getMessage());
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) SQLException(java.sql.SQLException)

Example 39 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class InitExceptionThrowTest method test_pool.

public void test_pool() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection();
    conn.close();
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection)

Example 40 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class MaxWaitTest method test_wait.

public void test_wait() throws Exception {
    Exception error = null;
    try {
        DruidPooledConnection conn = dataSource.getConnection();
        conn.close();
    } catch (SQLException ex) {
        error = ex;
    }
    assertTrue(error.getMessage().contains("createElapseMillis "));
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) SQLException(java.sql.SQLException)

Aggregations

DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)152 SQLException (java.sql.SQLException)99 PreparedStatement (java.sql.PreparedStatement)61 MockConnection (com.alibaba.druid.mock.MockConnection)49 Statement (java.sql.Statement)46 Connection (java.sql.Connection)20 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)10 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)7 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)5 ResultSet (java.sql.ResultSet)5 DruidPooledPreparedStatement (com.alibaba.druid.pool.DruidPooledPreparedStatement)4 PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)4 Properties (java.util.Properties)4 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)3 EncodingConvertFilter (com.alibaba.druid.filter.encoding.EncodingConvertFilter)2 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)2 MockResultSet (com.alibaba.druid.mock.MockResultSet)2 PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)2 PreparedStatementPool (com.alibaba.druid.pool.PreparedStatementPool)2 CountDownLatch (java.util.concurrent.CountDownLatch)2