List of usage examples for org.springframework.jdbc.support JdbcUtils closeStatement
public static void closeStatement(@Nullable Statement stmt)
From source file:lib.JdbcTemplate.java
@Override public <T> T execute(StatementCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); Connection con = DataSourceUtils.getConnection(getDataSource()); Statement stmt = null;/*from w w w. j a v a 2s .com*/ try { Connection conToUse = con; if (this.nativeJdbcExtractor != null && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) { conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } stmt = conToUse.createStatement(); applyStatementSettings(stmt); Statement stmtToUse = stmt; if (this.nativeJdbcExtractor != null) { stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt); } T result = action.doInStatement(stmtToUse); handleWarnings(stmt); return result; } catch (SQLException 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, getDataSource()); con = null; throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex); } finally { JdbcUtils.closeStatement(stmt); DataSourceUtils.releaseConnection(con, getDataSource()); } }
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 va 2 s .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:lib.JdbcTemplate.java
@Override public <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action) throws DataAccessException { Assert.notNull(psc, "PreparedStatementCreator must not be null"); Assert.notNull(action, "Callback object must not be null"); if (logger.isDebugEnabled()) { String sql = getSql(psc); logger.debug("Executing prepared SQL statement" + (sql != null ? " [" + sql + "]" : "")); }/*www . ja va 2s .co m*/ 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); applyStatementSettings(ps); PreparedStatement psToUse = ps; if (this.nativeJdbcExtractor != null) { psToUse = this.nativeJdbcExtractor.getNativePreparedStatement(ps); } T result = action.doInPreparedStatement(psToUse); handleWarnings(ps); return result; } catch (SQLException 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, getDataSource()); con = null; throw getExceptionTranslator().translate("PreparedStatementCallback", sql, ex); } finally { if (psc instanceof ParameterDisposer) { ((ParameterDisposer) psc).cleanupParameters(); } JdbcUtils.closeStatement(ps); DataSourceUtils.releaseConnection(con, getDataSource()); } }
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 ww w. j a v a 2s . com 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:it.doqui.index.ecmengine.business.personalization.hibernate.RoutingLocalSessionFactoryBean.java
/** * Execute the given schema script on the given JDBC Connection. * <p>Note that the default implementation will log unsuccessful statements * and continue to execute. Override the <code>executeSchemaStatement</code> * method to treat failures differently. * @param con the JDBC Connection to execute the script on * @param sql the SQL statements to execute * @throws SQLException if thrown by JDBC methods * @see #executeSchemaStatement//from w w w . ja v a 2s . c o m */ protected void executeSchemaScript(Connection con, String[] sql) throws SQLException { if (sql != null && sql.length > 0) { boolean oldAutoCommit = con.getAutoCommit(); if (!oldAutoCommit) { con.setAutoCommit(true); } try { Statement stmt = con.createStatement(); try { for (int i = 0; i < sql.length; i++) { executeSchemaStatement(stmt, sql[i]); } } finally { JdbcUtils.closeStatement(stmt); } } finally { if (!oldAutoCommit) { con.setAutoCommit(false); } } } }
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 w w w.java 2s.c o m*/ 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:lib.JdbcTemplate.java
@Override public <T> T execute(CallableStatementCreator csc, CallableStatementCallback<T> action) throws DataAccessException { Assert.notNull(csc, "CallableStatementCreator must not be null"); Assert.notNull(action, "Callback object must not be null"); if (logger.isDebugEnabled()) { String sql = getSql(csc); logger.debug("Calling stored procedure" + (sql != null ? " [" + sql + "]" : "")); }/* w w w. j a v a 2s . c o m*/ Connection con = DataSourceUtils.getConnection(getDataSource()); CallableStatement cs = null; try { Connection conToUse = con; if (this.nativeJdbcExtractor != null) { conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } cs = csc.createCallableStatement(conToUse); applyStatementSettings(cs); CallableStatement csToUse = cs; if (this.nativeJdbcExtractor != null) { csToUse = this.nativeJdbcExtractor.getNativeCallableStatement(cs); } T result = action.doInCallableStatement(csToUse); handleWarnings(cs); return result; } catch (SQLException 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, getDataSource()); con = null; throw getExceptionTranslator().translate("CallableStatementCallback", sql, ex); } finally { if (csc instanceof ParameterDisposer) { ((ParameterDisposer) csc).cleanupParameters(); } JdbcUtils.closeStatement(cs); DataSourceUtils.releaseConnection(con, getDataSource()); } }
From source file:org.springframework.jdbc.core.JdbcTemplate.java
public Object execute(final StatementCallback action) { Connection con = DataSourceUtils.getConnection(getDataSource()); Statement stmt = null;//from ww w .j a va 2 s .c om 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()); } }
From source file:org.springframework.jdbc.core.JdbcTemplate.java
public Object execute(PreparedStatementCreator psc, PreparedStatementCallback action) { //??//from w ww . j a v a 2 s. c o m 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()); } }
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 w w w .j a v a2s. c om 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()); } }