Search in sources :

Example 91 with DruidPooledConnection

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

the class PSCacheTest4 method test_pscache.

public void test_pscache() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection();
    DruidConnectionHolder holder = conn.getConnectionHolder();
    PreparedStatementPool stmtPool = holder.getStatementPool();
    final String sql_0 = "select 0";
    final String sql_1 = "select 1";
    Assert.assertEquals(0, stmtPool.size());
    PreparedStatementHolder stmtHoler_0;
    PreparedStatementHolder stmtHoler_1_A;
    PreparedStatementHolder stmtHoler_1_B;
    PreparedStatementHolder stmtHoler_1_C;
    DruidPooledPreparedStatement stmt_0;
    DruidPooledPreparedStatement stmt_1_A;
    DruidPooledPreparedStatement stmt_1_B;
    DruidPooledPreparedStatement stmt_1_C;
    DruidPooledPreparedStatement stmt_1_D;
    DruidPooledPreparedStatement stmt_1_E;
    DruidPooledPreparedStatement stmt_1_F;
    DruidPooledPreparedStatement stmt_1_G;
    DruidPooledPreparedStatement stmt_1_H;
    DruidPooledResultSet rs_0;
    DruidPooledResultSet rs_1_A;
    DruidPooledResultSet rs_1_B;
    DruidPooledResultSet rs_1_C;
    DruidPooledResultSet rs_1_D;
    DruidPooledResultSet rs_1_E;
    DruidPooledResultSet rs_1_F;
    DruidPooledResultSet rs_1_G;
    DruidPooledResultSet rs_1_H;
    stmt_0 = (DruidPooledPreparedStatement) conn.prepareStatement(sql_0);
    rs_0 = (DruidPooledResultSet) stmt_0.executeQuery();
    Assert.assertTrue(stmt_0.getPreparedStatementHolder().isInUse());
    stmt_1_A = (DruidPooledPreparedStatement) conn.prepareStatement(sql_1);
    rs_1_A = (DruidPooledResultSet) stmt_1_A.executeQuery();
    Assert.assertTrue(stmt_0.getPreparedStatementHolder().isInUse());
    Assert.assertTrue(stmt_1_A.getPreparedStatementHolder().isInUse());
    stmt_1_B = (DruidPooledPreparedStatement) conn.prepareStatement(sql_1);
    rs_1_B = (DruidPooledResultSet) stmt_1_B.executeQuery();
    rs_1_B.close();
    stmt_1_B.close();
    Assert.assertTrue(stmt_0.getPreparedStatementHolder().isInUse());
    Assert.assertTrue(stmt_1_A.getPreparedStatementHolder().isInUse());
    Assert.assertFalse(stmt_1_B.getPreparedStatementHolder().isInUse());
    stmt_1_C = (DruidPooledPreparedStatement) conn.prepareStatement(sql_1);
    rs_1_C = (DruidPooledResultSet) stmt_1_C.executeQuery();
    rs_1_C.close();
    stmt_1_C.close();
    Assert.assertTrue(stmt_0.getPreparedStatementHolder().isInUse());
    Assert.assertTrue(stmt_1_A.getPreparedStatementHolder().isInUse());
    Assert.assertFalse(stmt_1_B.getPreparedStatementHolder().isInUse());
    Assert.assertFalse(stmt_1_C.getPreparedStatementHolder().isInUse());
    stmt_1_D = (DruidPooledPreparedStatement) conn.prepareStatement(sql_1);
    rs_1_D = (DruidPooledResultSet) stmt_1_D.executeQuery();
    rs_1_D.close();
    stmt_1_D.close();
    Assert.assertTrue(stmt_0.getPreparedStatementHolder().isInUse());
    Assert.assertTrue(stmt_1_A.getPreparedStatementHolder().isInUse());
    Assert.assertFalse(stmt_1_B.getPreparedStatementHolder().isInUse());
    Assert.assertFalse(stmt_1_C.getPreparedStatementHolder().isInUse());
    Assert.assertFalse(stmt_1_D.getPreparedStatementHolder().isInUse());
    stmt_1_E = (DruidPooledPreparedStatement) conn.prepareStatement(sql_1);
    rs_1_E = (DruidPooledResultSet) stmt_1_E.executeQuery();
    rs_1_E.close();
    stmt_1_E.close();
    rs_1_A.close();
    stmt_1_A.close();
    stmt_1_F = (DruidPooledPreparedStatement) conn.prepareStatement(sql_1);
    rs_1_F = (DruidPooledResultSet) stmt_1_F.executeQuery();
    rs_1_F.close();
    stmt_1_F.close();
    stmt_1_G = (DruidPooledPreparedStatement) conn.prepareStatement(sql_1);
    rs_1_G = (DruidPooledResultSet) stmt_1_G.executeQuery();
    stmt_1_H = (DruidPooledPreparedStatement) conn.prepareStatement(sql_1);
    rs_1_H = (DruidPooledResultSet) stmt_1_H.executeQuery();
    rs_1_H.close();
    stmt_1_H.close();
    rs_1_G.close();
    stmt_1_G.close();
    conn.close();
}
Also used : PreparedStatementHolder(com.alibaba.druid.pool.PreparedStatementHolder) PreparedStatementPool(com.alibaba.druid.pool.PreparedStatementPool) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidPooledPreparedStatement(com.alibaba.druid.pool.DruidPooledPreparedStatement) DruidConnectionHolder(com.alibaba.druid.pool.DruidConnectionHolder) DruidPooledResultSet(com.alibaba.druid.pool.DruidPooledResultSet)

Example 92 with DruidPooledConnection

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

the class TestDruidDataSource method test_0.

public void test_0() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    Class.forName("com.alibaba.druid.mock.MockDriver");
    dataSource.setInitialSize(10);
    dataSource.setMaxActive(10);
    dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver");
    dataSource.setUrl("jdbc:mock:xxx");
    Assert.assertEquals(0, dataSource.getConnectCount());
    Assert.assertEquals(0, dataSource.getConnectErrorCount());
    Assert.assertEquals(0, dataSource.getCloseCount());
    Assert.assertEquals(0, dataSource.getPoolingCount());
    Connection conn = dataSource.getConnection();
    Assert.assertTrue(conn instanceof DruidPooledConnection);
    Assert.assertEquals(1, dataSource.getConnectCount());
    Assert.assertEquals(0, dataSource.getConnectErrorCount());
    Assert.assertEquals(0, dataSource.getCloseCount());
    Assert.assertEquals(0, dataSource.getRecycleCount());
    Assert.assertEquals(1, dataSource.getActiveCount());
    Assert.assertEquals(9, dataSource.getPoolingCount());
    conn.close();
    Assert.assertEquals(1, dataSource.getConnectCount());
    Assert.assertEquals(0, dataSource.getConnectErrorCount());
    Assert.assertEquals(1, dataSource.getCloseCount());
    Assert.assertEquals(1, dataSource.getRecycleCount());
    Assert.assertEquals(0, dataSource.getActiveCount());
    Assert.assertEquals(10, dataSource.getPoolingCount());
    // 重复close
    conn.close();
    Assert.assertEquals(1, dataSource.getConnectCount());
    Assert.assertEquals(0, dataSource.getConnectErrorCount());
    Assert.assertEquals(1, dataSource.getCloseCount());
    Assert.assertEquals(1, dataSource.getRecycleCount());
    Assert.assertEquals(0, dataSource.getActiveCount());
    dataSource.close();
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Connection(java.sql.Connection) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 93 with DruidPooledConnection

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

the class ConnectionTest4 method test_prepCall.

public void test_prepCall() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
    MockPreparedStatement raw = null;
    {
        PreparedStatement stmt = conn.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
        raw = stmt.unwrap(MockPreparedStatement.class);
        stmt.close();
    }
    {
        PreparedStatement stmt = conn.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
        Assert.assertEquals(raw, stmt.unwrap(MockPreparedStatement.class));
        stmt.close();
    }
    conn.close();
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatement(java.sql.PreparedStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement)

Example 94 with DruidPooledConnection

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

the class ConnectionTest4 method test_create_1.

public void test_create_1() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
    conn.getConnection().close();
    {
        SQLException error = null;
        try {
            conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    conn.close();
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection)

Example 95 with DruidPooledConnection

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

the class ConnectionTest4 method test_prepareStatement_error4.

public void test_prepareStatement_error4() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
    {
        SQLException error = null;
        try {
            conn.prepareStatement(null, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    conn.close();
}
Also used : SQLException(java.sql.SQLException) 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