List of usage examples for org.springframework.jdbc.core PreparedStatementCallback doInPreparedStatement
@Nullable
T doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException;
From source file:cc.tooyoung.common.db.JdbcTemplate.java
public Object execute(PreparedStatementCreator psc, PreparedStatementCallback action, boolean isWrite) throws DataAccessException { Assert.notNull(psc, "PreparedStatementCreator must not be null"); Assert.notNull(action, "Callback object must not be null"); if (ApiLogger.isTraceEnabled()) { String sql = getSql(psc); ApiLogger.trace(new StringBuilder(128).append("Executing prepared SQL statement") .append((sql != null ? " [" + sql + "]" : ""))); }/*from w ww.j a v a2s. c om*/ long start = System.currentTimeMillis(); DataSource ds = getDataSource(isWrite); Connection con = safeGetConnection(ds, isWrite); PreparedStatement ps = null; try { Connection conToUse = con; if (this.nativeJdbcExtractor != null && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativePreparedStatements()) { conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } ps = psc.createPreparedStatement(conToUse); applyStatementSettings(ds, ps); PreparedStatement psToUse = ps; if (this.nativeJdbcExtractor != null) { psToUse = this.nativeJdbcExtractor.getNativePreparedStatement(ps); } Object result = action.doInPreparedStatement(psToUse); handleWarnings(ps); 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 (psc instanceof ParameterDisposer) { ((ParameterDisposer) psc).cleanupParameters(); } String sql = getSql(psc); psc = null; JdbcUtils.closeStatement(ps); ps = null; DataSourceUtils.releaseConnection(con, ds); con = null; if (ex instanceof SQLException) { throw getExceptionTranslator(ds).translate("PreparedStatementCallback", sql, (SQLException) ex); } else { throw new RuntimeException("PreparedStatementCallback " + getSql(psc), 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 (psc instanceof ParameterDisposer) { ((ParameterDisposer) psc).cleanupParameters(); } JdbcUtils.closeStatement(ps); DataSourceUtils.releaseConnection(con, ds); TimeStatUtil.addElapseTimeStat(resource, isWrite, start, useTime); } }
From source file:org.springframework.jdbc.core.JdbcTemplate.java
public Object execute(PreparedStatementCreator psc, PreparedStatementCallback action) { //??//from www. j a va 2s .c om Connection con = DataSourceUtils.getConnection(getDataSource()); PreparedStatement ps = null; try { Connection conToUse = con; if (this.nativeJdbcExtractor != null && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativePreparedStatements()) { conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } ps = psc.createPreparedStatement(conToUse); DataSourceUtils.applyTransactionTimeout(ps, getDataSource()); PreparedStatement psToUse = ps; if (this.nativeJdbcExtractor != null) { psToUse = this.nativeJdbcExtractor.getNativePreparedStatement(ps); } // Object result = action.doInPreparedStatement(psToUse); SQLWarning warning = ps.getWarnings(); //??? throwExceptionOnWarningIfNotIgnoringWarnings(warning); return result; } catch (SQLException ex) { throw getExceptionTranslator().translate("executing PreparedStatementCallback [" + psc + "]", getSql(psc), ex); } finally {//? if (psc instanceof ParameterDisposer) { ((ParameterDisposer) psc).cleanupParameters(); } JdbcUtils.closeStatement(ps); DataSourceUtils.closeConnectionIfNecessary(con, getDataSource()); } }