Search in sources :

Example 51 with DruidPooledConnection

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

the class DruidPooledConnectionTest1 method test_dup_close.

public void test_dup_close() throws Exception {
    DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
    conn.close();
    conn.close();
    Assert.assertEquals(1, dataSource.getRecycleCount());
    Assert.assertEquals(1, dataSource.getCloseCount());
    Assert.assertEquals(1, dataSource.getPoolingCount());
    Assert.assertEquals(0, dataSource.getActiveCount());
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection)

Example 52 with DruidPooledConnection

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

the class DruidDataSourceTest_recycle2 method f_test_recycle_error_interrupt.

public void f_test_recycle_error_interrupt() throws Exception {
    final AtomicReference<Exception> errorRef = new AtomicReference<Exception>();
    final CountDownLatch closeBeforeLatch = new CountDownLatch(1);
    final CountDownLatch lockLatch = new CountDownLatch(1);
    final CountDownLatch endLatch = new CountDownLatch(1);
    Thread thread = new Thread() {

        public void run() {
            try {
                DruidPooledConnection conn = dataSource.getConnection();
                conn.setAutoCommit(false);
                conn.setReadOnly(false);
                Statement stmt = conn.createStatement();
                stmt.execute("select 1");
                Assert.assertEquals(0, dataSource.getPoolingCount());
                Assert.assertEquals(1, dataSource.getActiveCount());
                closeBeforeLatch.countDown();
                lockLatch.await();
                conn.close();
            } catch (Exception e) {
                errorRef.set(e);
            } finally {
                endLatch.countDown();
            }
        }
    };
    thread.start();
    Assert.assertTrue(closeBeforeLatch.await(1, TimeUnit.SECONDS));
    dataSource.getLock().lock();
    lockLatch.countDown();
    Thread.sleep(10);
    thread.interrupt();
    Assert.assertTrue(endLatch.await(1, TimeUnit.MINUTES));
    Exception error = errorRef.get();
    Assert.assertNull(error);
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Statement(java.sql.Statement) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) SQLException(java.sql.SQLException)

Example 53 with DruidPooledConnection

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

the class DruidDataSourceTest_recycle2 method test_recycle_error.

public void test_recycle_error() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection();
    conn.setAutoCommit(false);
    conn.setReadOnly(false);
    Statement stmt = conn.createStatement();
    stmt.execute("select 1");
    Assert.assertEquals(0, dataSource.getPoolingCount());
    Assert.assertEquals(1, dataSource.getActiveCount());
    Exception error = null;
    try {
        conn.close();
    } catch (Exception e) {
        error = e;
    }
    Assert.assertNull(error);
    {
        Connection conn2 = dataSource.getConnection();
        conn2.close();
    }
    Assert.assertEquals(1, dataSource.getPoolingCount());
    Assert.assertEquals(0, dataSource.getActiveCount());
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Statement(java.sql.Statement) Connection(java.sql.Connection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) SQLException(java.sql.SQLException)

Example 54 with DruidPooledConnection

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

the class DruidDataSourceTest_recycle method test_recycle.

public void test_recycle() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection();
    conn.setAutoCommit(false);
    conn.setReadOnly(false);
    Statement stmt = conn.createStatement();
    stmt.execute("select 1");
    conn.close();
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Statement(java.sql.Statement)

Example 55 with DruidPooledConnection

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

the class DruidDataSourceTest_tryGet method test_0.

public void test_0() throws Exception {
    DruidPooledConnection conn1 = this.dataSource.tryGetConnection();
    Assert.assertNotNull(conn1);
    DruidPooledConnection conn2 = this.dataSource.tryGetConnection();
    Assert.assertNull(conn2);
    conn1.close();
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection)

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