Example usage for org.springframework.jdbc.core CallableStatementCreator createCallableStatement

List of usage examples for org.springframework.jdbc.core CallableStatementCreator createCallableStatement

Introduction

In this page you can find the example usage for org.springframework.jdbc.core CallableStatementCreator createCallableStatement.

Prototype

CallableStatement createCallableStatement(Connection con) throws SQLException;

Source Link

Document

Create a callable statement in this connection.

Usage

From source file:cc.tooyoung.common.db.JdbcTemplate.java

public Object execute(CallableStatementCreator csc, CallableStatementCallback action, boolean isWrite)
        throws DataAccessException {

    Assert.notNull(csc, "CallableStatementCreator must not be null");
    Assert.notNull(action, "Callback object must not be null");
    if (ApiLogger.isTraceEnabled()) {
        String sql = getSql(csc);
        ApiLogger.trace("Calling stored procedure" + (sql != null ? " [" + sql + "]" : ""));
    }/*from   www.  j a  v  a 2  s .  c om*/

    long start = System.currentTimeMillis();
    DataSource ds = getDataSource(isWrite);
    Connection con = safeGetConnection(ds, isWrite);
    CallableStatement cs = null;
    try {
        Connection conToUse = con;
        if (this.nativeJdbcExtractor != null) {
            conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
        }
        cs = csc.createCallableStatement(conToUse);
        applyStatementSettings(ds, cs);
        CallableStatement csToUse = cs;
        if (this.nativeJdbcExtractor != null) {
            csToUse = this.nativeJdbcExtractor.getNativeCallableStatement(cs);
        }
        Object result = action.doInCallableStatement(csToUse);
        handleWarnings(cs);
        return result;
    } catch (Exception ex) {
        // Release Connection early, to avoid potential connection pool deadlock
        // in the case when the exception translator hasn't been initialized yet.
        if (csc instanceof ParameterDisposer) {
            ((ParameterDisposer) csc).cleanupParameters();
        }
        String sql = getSql(csc);
        csc = null;
        JdbcUtils.closeStatement(cs);
        cs = null;
        DataSourceUtils.releaseConnection(con, ds);
        con = null;
        if (ex instanceof SQLException) {
            throw getExceptionTranslator(ds).translate("CallableStatementCallback", sql, (SQLException) ex);
        } else {
            throw new RuntimeException("CallableStatementCallback " + getSql(csc), ex);
        }
    } finally {
        //add slow log
        long useTime = System.currentTimeMillis() - start;
        if (useTime > ApiLogger.DB_FIRE_TIME) {
            ApiLogger.fire(new StringBuffer().append("DB ")
                    .append(((com.mchange.v2.c3p0.ComboPooledDataSource) ds).getJdbcUrl()).append(" too slow :")
                    .append(useTime).append(" isWrite:").append(isWrite));
        }

        if (csc instanceof ParameterDisposer) {
            ((ParameterDisposer) csc).cleanupParameters();
        }
        JdbcUtils.closeStatement(cs);
        DataSourceUtils.releaseConnection(con, ds);

        TimeStatUtil.addElapseTimeStat(resource, isWrite, start, useTime);
    }
}

From source file:org.springframework.jdbc.core.JdbcTemplate.java

public Object execute(CallableStatementCreator csc, CallableStatementCallback action) {
    if (logger.isDebugEnabled()) {
        String sql = getSql(csc);
        logger.debug("Calling stored procedure" + (sql != null ? " [" + sql + "]" : ""));
    }//from   ww  w . j  a  va 2s . c  o m
    Connection con = DataSourceUtils.getConnection(getDataSource());
    CallableStatement cs = null;
    try {
        Connection conToUse = con;
        if (this.nativeJdbcExtractor != null
                && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeCallableStatements()) {
            conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
        }
        cs = csc.createCallableStatement(conToUse);
        DataSourceUtils.applyTransactionTimeout(cs, getDataSource());
        CallableStatement csToUse = cs;
        if (this.nativeJdbcExtractor != null) {
            csToUse = this.nativeJdbcExtractor.getNativeCallableStatement(cs);
        }
        Object result = action.doInCallableStatement(csToUse);
        SQLWarning warning = cs.getWarnings();
        throwExceptionOnWarningIfNotIgnoringWarnings(warning);
        return result;
    } catch (SQLException ex) {
        throw getExceptionTranslator().translate("executing CallableStatementCallback [" + csc + "]",
                getSql(csc), ex);
    } finally {
        if (csc instanceof ParameterDisposer) {
            ((ParameterDisposer) csc).cleanupParameters();
        }
        JdbcUtils.closeStatement(cs);
        DataSourceUtils.closeConnectionIfNecessary(con, getDataSource());
    }
}