Search in sources :

Example 1 with PreparedStatementHolder

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

the class PoolableCallableStatementTest method setUp.

protected void setUp() throws Exception {
    MockConnection mockConn = new MockConnection();
    DruidConnectionHolder connHolder = new DruidConnectionHolder(dataSource, mockConn, 0);
    conn = new DruidPooledConnection(connHolder);
    raw = new MockCallableStatement(null, null);
    stmt = new DruidPooledCallableStatement(conn, new PreparedStatementHolder(new PreparedStatementKey("", null, null, 0, 0, 0), raw)) {

        protected SQLException checkException(Throwable error) throws SQLException {
            if (error instanceof SQLException) {
                return (SQLException) error;
            }
            return new SQLException(error);
        }
    };
    Assert.assertEquals(0, raw.getOutParameters().size());
    stmt.registerOutParameter(1, Types.INTEGER);
    Assert.assertEquals(1, raw.getOutParameters().size());
    stmt.registerOutParameter(2, Types.DECIMAL, 10);
    Assert.assertEquals(2, raw.getOutParameters().size());
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) PreparedStatementHolder(com.alibaba.druid.pool.PreparedStatementHolder) DruidPooledCallableStatement(com.alibaba.druid.pool.DruidPooledCallableStatement) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidConnectionHolder(com.alibaba.druid.pool.DruidConnectionHolder) PreparedStatementKey(com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 2 with PreparedStatementHolder

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

the class PreparedStatementKeyTest method test_contains.

public void test_contains() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    MockConnection conn = new MockConnection();
    PreparedStatementKey k1 = new PreparedStatementKey("x1", "c1", MethodType.M1);
    PreparedStatementPool pool = new PreparedStatementPool(new DruidConnectionHolder(dataSource, conn, 0));
    MockPreparedStatement raw = new MockPreparedStatement(null, null);
    pool.put(new PreparedStatementHolder(k1, raw));
    Assert.assertTrue(pool.get(k1) != null);
    Assert.assertTrue(pool.get(k1) != null);
}
Also used : PreparedStatementHolder(com.alibaba.druid.pool.PreparedStatementHolder) PreparedStatementPool(com.alibaba.druid.pool.PreparedStatementPool) PreparedStatementKey(com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey) DruidConnectionHolder(com.alibaba.druid.pool.DruidConnectionHolder) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 3 with PreparedStatementHolder

use of com.alibaba.druid.pool.PreparedStatementHolder 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 4 with PreparedStatementHolder

use of com.alibaba.druid.pool.PreparedStatementHolder 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 5 with PreparedStatementHolder

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

the class TestOraclePrefetch method test_oracle.

public void test_oracle() throws Exception {
    String sql = "SELECT 1";
    OracleConnection oracleConn;
    OraclePreparedStatement oracleStmt;
    PreparedStatementHolder stmtHolder;
    {
        Connection conn = dataSource.getConnection();
        {
            oracleConn = conn.unwrap(OracleConnection.class);
            Assert.assertEquals(50, oracleConn.getDefaultRowPrefetch());
        }
        PreparedStatement stmt = conn.prepareStatement(sql);
        oracleStmt = stmt.unwrap(OraclePreparedStatement.class);
        Assert.assertEquals(50, oracleStmt.getRowPrefetch());
        Assert.assertTrue(stmt.isWrapperFor(PreparedStatementHolder.class));
        stmtHolder = stmt.unwrap(PreparedStatementHolder.class);
        Assert.assertNotNull(stmtHolder);
        Assert.assertEquals(0, stmtHolder.getHitCount());
        ResultSet rs = stmt.executeQuery();
        rs.next();
        rs.close();
        stmt.close();
        conn.close();
    }
    {
        Connection conn = dataSource.getConnection();
        {
            OracleConnection oracleConn2 = conn.unwrap(OracleConnection.class);
            Assert.assertEquals(50, oracleConn2.getDefaultRowPrefetch());
            Assert.assertSame(oracleConn, oracleConn2);
        }
        PreparedStatement stmt = conn.prepareStatement(sql);
        {
            PreparedStatementHolder stmtHolder2 = stmt.unwrap(PreparedStatementHolder.class);
            Assert.assertSame(stmtHolder2, stmtHolder);
            Assert.assertEquals(1, stmtHolder.getHitCount());
        }
        ResultSet rs = stmt.executeQuery();
        rs.next();
        rs.close();
        stmt.close();
        {
            OraclePreparedStatement oracleStmt2 = stmt.unwrap(OraclePreparedStatement.class);
            Assert.assertSame(oracleStmt, oracleStmt2);
            Assert.assertEquals(2, oracleStmt.getRowPrefetch());
        }
        conn.close();
    }
    Assert.assertEquals(1, dataSource.getCachedPreparedStatementCount());
}
Also used : PreparedStatementHolder(com.alibaba.druid.pool.PreparedStatementHolder) Connection(java.sql.Connection) OracleConnection(oracle.jdbc.OracleConnection) ResultSet(java.sql.ResultSet) OraclePreparedStatement(oracle.jdbc.internal.OraclePreparedStatement) OraclePreparedStatement(oracle.jdbc.internal.OraclePreparedStatement) PreparedStatement(java.sql.PreparedStatement) OracleConnection(oracle.jdbc.OracleConnection)

Aggregations

PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)6 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)5 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)4 MockConnection (com.alibaba.druid.mock.MockConnection)3 DruidPooledPreparedStatement (com.alibaba.druid.pool.DruidPooledPreparedStatement)3 PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)3 PreparedStatementPool (com.alibaba.druid.pool.PreparedStatementPool)3 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)2 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)2 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)1 DruidPooledCallableStatement (com.alibaba.druid.pool.DruidPooledCallableStatement)1 DruidPooledResultSet (com.alibaba.druid.pool.DruidPooledResultSet)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 OracleConnection (oracle.jdbc.OracleConnection)1 OraclePreparedStatement (oracle.jdbc.internal.OraclePreparedStatement)1