Example usage for org.springframework.jdbc.datasource DataSourceUtils applyTransactionTimeout

List of usage examples for org.springframework.jdbc.datasource DataSourceUtils applyTransactionTimeout

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource DataSourceUtils applyTransactionTimeout.

Prototype

public static void applyTransactionTimeout(Statement stmt, @Nullable DataSource dataSource)
        throws SQLException 

Source Link

Document

Apply the current transaction timeout, if any, to the given JDBC Statement object.

Usage

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

public Object execute(final StatementCallback action) {
    Connection con = DataSourceUtils.getConnection(getDataSource());
    Statement stmt = null;//  ww w .  j  av a 2s  . co m
    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  w  w . j  a  v a  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);
        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 + "]" : ""));
    }/*w w w  .  jav a 2  s  .  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());
    }
}