Search in sources :

Example 1 with DruidPooledPreparedStatement

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

the class PSCacheTest method test_txn.

public void test_txn() throws Exception {
    Connection conn = dataSource.getConnection();
    conn.setAutoCommit(false);
    String sql = "select 1";
    PreparedStatement stmt0 = conn.prepareStatement(sql);
    DruidPooledPreparedStatement pooledStmt0 = (DruidPooledPreparedStatement) stmt0;
    stmt0.close();
    PreparedStatement stmt1 = conn.prepareStatement(sql);
    DruidPooledPreparedStatement pooledStmt1 = (DruidPooledPreparedStatement) stmt1;
    Assert.assertEquals(1, pooledStmt1.getPreparedStatementHolder().getInUseCount());
    // same
    Assert.assertSame(pooledStmt1.getPreparedStatementHolder(), pooledStmt0.getPreparedStatementHolder());
    PreparedStatement stmt2 = conn.prepareStatement(sql);
    DruidPooledPreparedStatement pooledStmt2 = (DruidPooledPreparedStatement) stmt2;
    // not same
    Assert.assertNotSame(pooledStmt1.getPreparedStatementHolder(), pooledStmt2.getPreparedStatementHolder());
    stmt1.close();
    stmt2.close();
    conn.close();
}
Also used : Connection(java.sql.Connection) DruidPooledPreparedStatement(com.alibaba.druid.pool.DruidPooledPreparedStatement) PreparedStatement(java.sql.PreparedStatement) DruidPooledPreparedStatement(com.alibaba.druid.pool.DruidPooledPreparedStatement)

Example 2 with DruidPooledPreparedStatement

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

the class PSCacheTest2 method test_0.

public void test_0() throws Exception {
    Connection conn = dataSource.getConnection();
    String sql = "select 1";
    {
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.close();
    }
    PreparedStatement stmt0 = conn.prepareStatement(sql);
    DruidPooledPreparedStatement pooledStmt0 = (DruidPooledPreparedStatement) stmt0;
    Assert.assertEquals(1, pooledStmt0.getPreparedStatementHolder().getInUseCount());
    PreparedStatement stmt1 = conn.prepareStatement(sql);
    DruidPooledPreparedStatement pooledStmt1 = (DruidPooledPreparedStatement) stmt1;
    Assert.assertSame(pooledStmt0.getPreparedStatementHolder(), pooledStmt1.getPreparedStatementHolder());
    stmt0.close();
    stmt1.close();
    conn.close();
}
Also used : Connection(java.sql.Connection) DruidPooledPreparedStatement(com.alibaba.druid.pool.DruidPooledPreparedStatement) PreparedStatement(java.sql.PreparedStatement) DruidPooledPreparedStatement(com.alibaba.druid.pool.DruidPooledPreparedStatement)

Example 3 with DruidPooledPreparedStatement

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

the class PSCacheTest2 method test_txn.

public void test_txn() throws Exception {
    Connection conn = dataSource.getConnection();
    conn.setAutoCommit(true);
    String sql = "select 1";
    {
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.close();
    }
    PreparedStatement stmt0 = conn.prepareStatement(sql);
    DruidPooledPreparedStatement pooledStmt0 = (DruidPooledPreparedStatement) stmt0;
    Assert.assertEquals(1, pooledStmt0.getPreparedStatementHolder().getInUseCount());
    PreparedStatement stmt1 = conn.prepareStatement(sql);
    DruidPooledPreparedStatement pooledStmt1 = (DruidPooledPreparedStatement) stmt1;
    Assert.assertSame(pooledStmt0.getPreparedStatementHolder(), pooledStmt1.getPreparedStatementHolder());
    stmt0.close();
    stmt1.close();
    conn.close();
}
Also used : Connection(java.sql.Connection) DruidPooledPreparedStatement(com.alibaba.druid.pool.DruidPooledPreparedStatement) PreparedStatement(java.sql.PreparedStatement) DruidPooledPreparedStatement(com.alibaba.druid.pool.DruidPooledPreparedStatement)

Example 4 with DruidPooledPreparedStatement

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

the class PSCacheTest3 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";
    final String sql_2 = "select 2";
    final String sql_3 = "select 3";
    final String sql_4 = "select 4";
    Assert.assertEquals(0, stmtPool.size());
    PreparedStatementHolder stmtHoler_0;
    PreparedStatementHolder stmtHoler_1;
    PreparedStatementHolder stmtHoler_2;
    PreparedStatementHolder stmtHoler_3;
    PreparedStatementHolder stmtHoler_4;
    // sql_0连续执行两次
    {
        DruidPooledPreparedStatement stmt_0 = (DruidPooledPreparedStatement) conn.prepareStatement(sql_0);
        Assert.assertFalse(stmt_0.getPreparedStatementHolder().isPooling());
        stmt_0.close();
        Assert.assertEquals(1, stmtPool.size());
        Assert.assertTrue(stmt_0.getPreparedStatementHolder().isPooling());
    }
    {
        DruidPooledPreparedStatement stmt_0 = (DruidPooledPreparedStatement) conn.prepareStatement(sql_0);
        Assert.assertEquals(1, stmtPool.size());
        Assert.assertTrue(stmt_0.getPreparedStatementHolder().isInUse());
        Assert.assertTrue(stmt_0.getPreparedStatementHolder().isPooling());
        stmt_0.close();
        Assert.assertFalse(stmt_0.getPreparedStatementHolder().isInUse());
        Assert.assertTrue(stmt_0.getPreparedStatementHolder().isPooling());
        Assert.assertEquals(1, stmtPool.size());
    }
    DruidPooledPreparedStatement stmt_0 = (DruidPooledPreparedStatement) conn.prepareStatement(sql_0);
    stmtHoler_0 = stmt_0.getPreparedStatementHolder();
    Assert.assertTrue(stmtHoler_0.isInUse());
    Assert.assertTrue(stmtHoler_0.isPooling());
    stmt_0.execute();
    {
        DruidPooledPreparedStatement stmt_1 = (DruidPooledPreparedStatement) conn.prepareStatement(sql_1);
        Assert.assertEquals(1, stmtPool.size());
        stmtHoler_1 = stmt_1.getPreparedStatementHolder();
        Assert.assertTrue(stmt_1.getPreparedStatementHolder().isInUse());
        Assert.assertFalse(stmt_1.getPreparedStatementHolder().isPooling());
        stmt_1.close();
        Assert.assertFalse(stmt_1.getPreparedStatementHolder().isInUse());
        Assert.assertTrue(stmt_1.getPreparedStatementHolder().isPooling());
        Assert.assertTrue(stmt_1.getPreparedStatementHolder().isPooling());
        Assert.assertEquals(2, stmtPool.size());
    }
    Assert.assertTrue(stmtHoler_0.isPooling());
    Assert.assertTrue(stmtHoler_1.isPooling());
    {
        DruidPooledPreparedStatement stmt_2 = (DruidPooledPreparedStatement) conn.prepareStatement(sql_2);
        Assert.assertEquals(2, stmtPool.size());
        stmtHoler_2 = stmt_2.getPreparedStatementHolder();
        Assert.assertTrue(stmt_2.getPreparedStatementHolder().isInUse());
        Assert.assertFalse(stmt_2.getPreparedStatementHolder().isPooling());
        stmt_2.close();
        Assert.assertFalse(stmt_2.getPreparedStatementHolder().isInUse());
        Assert.assertTrue(stmt_2.getPreparedStatementHolder().isPooling());
        Assert.assertTrue(stmt_2.getPreparedStatementHolder().isPooling());
        Assert.assertEquals(3, stmtPool.size());
    }
    Assert.assertTrue(stmtHoler_0.isPooling());
    Assert.assertTrue(stmtHoler_1.isPooling());
    Assert.assertTrue(stmtHoler_2.isPooling());
    {
        DruidPooledPreparedStatement stmt_3 = (DruidPooledPreparedStatement) conn.prepareStatement(sql_3);
        Assert.assertEquals(3, stmtPool.size());
        stmtHoler_3 = stmt_3.getPreparedStatementHolder();
        Assert.assertTrue(stmt_3.getPreparedStatementHolder().isInUse());
        Assert.assertFalse(stmt_3.getPreparedStatementHolder().isPooling());
        stmt_3.close();
        Assert.assertFalse(stmt_3.getPreparedStatementHolder().isInUse());
        Assert.assertTrue(stmt_3.getPreparedStatementHolder().isPooling());
        Assert.assertTrue(stmt_3.getPreparedStatementHolder().isPooling());
        Assert.assertEquals(3, stmtPool.size());
    }
    Assert.assertFalse(stmtHoler_0.isPooling());
    Assert.assertTrue(stmtHoler_1.isPooling());
    Assert.assertTrue(stmtHoler_2.isPooling());
    Assert.assertTrue(stmtHoler_3.isPooling());
    {
        DruidPooledPreparedStatement stmt_4 = (DruidPooledPreparedStatement) conn.prepareStatement(sql_4);
        Assert.assertEquals(3, stmtPool.size());
        stmtHoler_4 = stmt_4.getPreparedStatementHolder();
        Assert.assertTrue(stmt_4.getPreparedStatementHolder().isInUse());
        Assert.assertFalse(stmt_4.getPreparedStatementHolder().isPooling());
        stmt_4.close();
        Assert.assertFalse(stmt_4.getPreparedStatementHolder().isInUse());
        Assert.assertTrue(stmt_4.getPreparedStatementHolder().isPooling());
        Assert.assertTrue(stmt_4.getPreparedStatementHolder().isPooling());
        Assert.assertEquals(3, stmtPool.size());
    }
    Assert.assertFalse(stmtHoler_0.isPooling());
    Assert.assertFalse(stmtHoler_1.isPooling());
    Assert.assertTrue(stmtHoler_2.isPooling());
    Assert.assertTrue(stmtHoler_3.isPooling());
    Assert.assertTrue(stmtHoler_4.isPooling());
    stmt_0.close();
    Assert.assertTrue(stmtHoler_0.isPooling());
    Assert.assertFalse(stmtHoler_1.isPooling());
    Assert.assertFalse(stmtHoler_2.isPooling());
    Assert.assertTrue(stmtHoler_3.isPooling());
    Assert.assertTrue(stmtHoler_4.isPooling());
    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)

Example 5 with DruidPooledPreparedStatement

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

the class DruidPooledConnectionTest1 method test_closePoolableStatement.

public void test_closePoolableStatement() throws Exception {
    DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
    DruidPooledPreparedStatement stmt = (DruidPooledPreparedStatement) conn.prepareStatement("select 1");
    conn.close();
    conn.closePoolableStatement(stmt);
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidPooledPreparedStatement(com.alibaba.druid.pool.DruidPooledPreparedStatement)

Aggregations

DruidPooledPreparedStatement (com.alibaba.druid.pool.DruidPooledPreparedStatement)8 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)3 PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)3 PreparedStatementPool (com.alibaba.druid.pool.PreparedStatementPool)2 MockConnection (com.alibaba.druid.mock.MockConnection)1 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)1 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)1 DruidPooledResultSet (com.alibaba.druid.pool.DruidPooledResultSet)1 SQLException (java.sql.SQLException)1