Search in sources :

Example 1 with PreparedStatementKey

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

the class DruidPooledConnection method prepareCall.

// ////////////////////
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
    checkState();
    PreparedStatementHolder stmtHolder = null;
    PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.Precall_1);
    boolean poolPreparedStatements = holder.isPoolPreparedStatements();
    if (poolPreparedStatements) {
        stmtHolder = holder.getStatementPool().get(key);
    }
    if (stmtHolder == null) {
        try {
            stmtHolder = new PreparedStatementHolder(key, conn.prepareCall(sql));
            holder.getDataSource().incrementPreparedStatementCount();
        } catch (SQLException ex) {
            handleException(ex);
        }
    }
    initStatement(stmtHolder);
    DruidPooledCallableStatement rtnVal = new DruidPooledCallableStatement(this, stmtHolder);
    holder.addTrace(rtnVal);
    return rtnVal;
}
Also used : SQLException(java.sql.SQLException) PreparedStatementKey(com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)

Example 2 with PreparedStatementKey

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

the class DruidPooledConnection method prepareStatement.

// ////////////////////
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
    checkState();
    PreparedStatementHolder stmtHolder = null;
    PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M1);
    boolean poolPreparedStatements = holder.isPoolPreparedStatements();
    if (poolPreparedStatements) {
        stmtHolder = holder.getStatementPool().get(key);
    }
    if (stmtHolder == null) {
        try {
            stmtHolder = new PreparedStatementHolder(key, conn.prepareStatement(sql));
            holder.getDataSource().incrementPreparedStatementCount();
        } catch (SQLException ex) {
            handleException(ex);
        }
    }
    initStatement(stmtHolder);
    DruidPooledPreparedStatement rtnVal = new DruidPooledPreparedStatement(this, stmtHolder);
    holder.addTrace(rtnVal);
    return rtnVal;
}
Also used : SQLException(java.sql.SQLException) PreparedStatementKey(com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)

Example 3 with PreparedStatementKey

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

the class DruidPooledConnection method prepareStatement.

@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
    checkState();
    PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M4, columnIndexes);
    PreparedStatementHolder stmtHolder = null;
    boolean poolPreparedStatements = holder.isPoolPreparedStatements();
    if (poolPreparedStatements) {
        stmtHolder = holder.getStatementPool().get(key);
    }
    if (stmtHolder == null) {
        try {
            stmtHolder = new PreparedStatementHolder(key, conn.prepareStatement(sql, columnIndexes));
            holder.getDataSource().incrementPreparedStatementCount();
        } catch (SQLException ex) {
            handleException(ex);
        }
    }
    initStatement(stmtHolder);
    DruidPooledPreparedStatement rtnVal = new DruidPooledPreparedStatement(this, stmtHolder);
    holder.addTrace(rtnVal);
    return rtnVal;
}
Also used : SQLException(java.sql.SQLException) PreparedStatementKey(com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)

Example 4 with PreparedStatementKey

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

the class PreparedStatementKeyTest method test_0.

public void test_0() throws Exception {
    PreparedStatementKey k1 = new PreparedStatementKey("select 'a'", "c1", MethodType.M1, 101, 102, 103);
    Assert.assertEquals(101, k1.getResultSetType());
    Assert.assertEquals(102, k1.getResultSetConcurrency());
    Assert.assertEquals(103, k1.getResultSetHoldability());
}
Also used : PreparedStatementKey(com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)

Example 5 with PreparedStatementKey

use of com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey 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) SQLException(java.sql.SQLException) 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)

Aggregations

PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)19 SQLException (java.sql.SQLException)8 MockConnection (com.alibaba.druid.mock.MockConnection)3 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)3 PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)3 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)2 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)2 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)1 DruidPooledCallableStatement (com.alibaba.druid.pool.DruidPooledCallableStatement)1 DruidPooledPreparedStatement (com.alibaba.druid.pool.DruidPooledPreparedStatement)1 PreparedStatementPool (com.alibaba.druid.pool.PreparedStatementPool)1