Example usage for org.springframework.jdbc.core StatementCallback doInStatement

List of usage examples for org.springframework.jdbc.core StatementCallback doInStatement

Introduction

In this page you can find the example usage for org.springframework.jdbc.core StatementCallback doInStatement.

Prototype

@Nullable
T doInStatement(Statement stmt) throws SQLException, DataAccessException;

Source Link

Document

Gets called by JdbcTemplate.execute with an active JDBC Statement.

Usage

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

public Object execute(StatementCallback action, boolean isWrite) throws DataAccessException {
    Assert.notNull(action, "Callback object must not be null");

    long start = System.currentTimeMillis();

    DataSource ds = getDataSource(isWrite);
    Connection con = safeGetConnection(ds, isWrite);
    Statement stmt = null;//from   w  w w  . j  a v  a2s.c om
    try {
        Connection conToUse = con;
        if (this.nativeJdbcExtractor != null
                && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {
            conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
        }
        stmt = conToUse.createStatement();
        applyStatementSettings(ds, stmt);
        Statement stmtToUse = stmt;
        if (this.nativeJdbcExtractor != null) {
            stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);
        }
        Object result = action.doInStatement(stmtToUse);
        handleWarnings(stmt);
        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.
        JdbcUtils.closeStatement(stmt);
        stmt = null;
        DataSourceUtils.releaseConnection(con, ds);
        con = null;
        if (ex instanceof SQLException) {
            throw getExceptionTranslator(ds).translate("StatementCallback", getSql(action), (SQLException) ex);
        } else {
            throw new RuntimeException("StatementCallback " + getSql(action), ex);
        }
    } finally {
        JdbcUtils.closeStatement(stmt);
        DataSourceUtils.releaseConnection(con, ds);

        //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));
        }

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

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

public Object execute(final StatementCallback action) {
    Connection con = DataSourceUtils.getConnection(getDataSource());
    Statement stmt = null;/*w  w  w .ja  va  2  s  . com*/
    try {
        Connection conToUse = con;
        if (this.nativeJdbcExtractor != null
                && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {
            conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
        }
        stmt = conToUse.createStatement();
        DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
        Statement stmtToUse = stmt;
        if (this.nativeJdbcExtractor != null) {
            stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);
        }
        Object result = action.doInStatement(stmtToUse);
        SQLWarning warning = stmt.getWarnings();
        throwExceptionOnWarningIfNotIgnoringWarnings(warning);
        return result;
    } catch (SQLException ex) {
        throw getExceptionTranslator().translate("executing StatementCallback", getSql(action), ex);
    } finally {
        JdbcUtils.closeStatement(stmt);
        DataSourceUtils.closeConnectionIfNecessary(con, getDataSource());
    }
}