List of usage examples for java.sql PreparedStatement setQueryTimeout
void setQueryTimeout(int seconds) throws SQLException;
Statement
object to execute to the given number of seconds. From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java
/** * Allow subclasses to provide DB unique override implementations of * setting query timeouts, while preserving the default timeout logic * in the public setQueryTimeout method. * @param stmnt// www . j a v a2 s . c om * @param timeout in milliseconds * @throws SQLException */ protected void setStatementQueryTimeout(PreparedStatement stmnt, int timeout) throws SQLException { // JDBC uses seconds, so we'll do a simple round-down conversion here stmnt.setQueryTimeout(timeout / 1000); }
From source file:org.bidtime.dbutils.QueryRunnerEx.java
/** * Calls update after checking the parameters to ensure nothing is null. * @param conn The connection to use for the batch call. * @param closeConn True if the connection should be closed, false otherwise. * @param sql The SQL statement to execute. * @param params An array of query replacement parameters. Each row in * this array is one set of batch replacement values. * @return The number of rows updated in the batch. * @throws SQLException If there are database or parameter errors. */// w w w.ja va 2 s . c o m private int[] batch(Connection conn, boolean closeConn, String sql, Object[][] params) throws SQLException { if (conn == null) { throw new SQLException("Null connection"); } if (sql == null) { if (closeConn) { close(conn); } throw new SQLException("Null SQL statement"); } if (params == null) { if (closeConn) { close(conn); } throw new SQLException("Null parameters. If parameters aren't need, pass an empty array."); } long startTime = System.currentTimeMillis(); PreparedStatement stmt = null; int[] rows = null; try { stmt = this.prepareStatement(conn, sql); stmt.setQueryTimeout(StmtParams.getInstance().getStmtBatchTimeOut()); for (int i = 0; i < params.length; i++) { this.fillStatement(stmt, params[i]); stmt.addBatch(); } rows = stmt.executeBatch(); } catch (SQLException e) { this.rethrow(e, sql, (Object[]) params); } finally { close(stmt); if (closeConn) { close(conn); } if (LogUpdateSql.logInfoOrDebug()) { LogUpdateSql.logFormatTimeNow(startTime, sql, params, (rows != null ? rows.length : 0)); } } return rows; }
From source file:org.bidtime.dbutils.QueryRunnerEx.java
/** * Calls query after checking the parameters to ensure nothing is null. * @param conn The connection to use for the query call. * @param closeConn True if the connection should be closed, false otherwise. * @param sql The SQL statement to execute. * @param params An array of query replacement parameters. Each row in * this array is one set of batch replacement values. * @return The results of the query.//from www. ja v a 2 s. c om * @throws SQLException If there are database or parameter errors. */ private <T> T query(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException { if (conn == null) { throw new SQLException("Null connection"); } if (sql == null) { if (closeConn) { close(conn); } throw new SQLException("Null SQL statement"); } if (rsh == null) { if (closeConn) { close(conn); } throw new SQLException("Null ResultSetHandler"); } PreparedStatement stmt = null; ResultSet rs = null; T result = null; long startTime = System.currentTimeMillis(); try { //stmt = this.prepareStatement(conn, sql); stmt = (PreparedStatement) conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setFetchSize(StmtParams.getInstance().getFetchSize()); stmt.setFetchDirection(ResultSet.FETCH_REVERSE); stmt.setQueryTimeout(StmtParams.getInstance().getStmtQueryTimeOut()); this.fillStatement(stmt, params); rs = this.wrap(stmt.executeQuery()); result = rsh.handle(rs); } catch (SQLException e) { this.rethrow(e, sql, params); } finally { try { close(rs); } finally { close(stmt); if (closeConn) { close(conn); } } if (LogSelectSql.logInfoOrDebug()) { LogSelectSql.logFormatTimeNow(startTime, sql, params); } } return result; }
From source file:org.bidtime.dbutils.QueryRunnerEx.java
/** * Calls update after checking the parameters to ensure nothing is null. * @param conn The connection to use for the update call. * @param closeConn True if the connection should be closed, false otherwise. * @param sql The SQL statement to execute. * @param params An array of update replacement parameters. Each row in * this array is one set of update replacement values. * @return The number of rows updated.//from www .j a v a2 s . c o m * @throws SQLException If there are database or parameter errors. */ private int update(Connection conn, boolean closeConn, String sql, Object... params) throws SQLException { if (conn == null) { throw new SQLException("Null connection"); } if (sql == null) { if (closeConn) { close(conn); } throw new SQLException("Null SQL statement"); } PreparedStatement stmt = null; int rows = 0; long startTime = System.currentTimeMillis(); try { stmt = this.prepareStatement(conn, sql); stmt.setQueryTimeout(StmtParams.getInstance().getStmtUpdateTimeOut()); this.fillStatement(stmt, params); rows = stmt.executeUpdate(); } catch (SQLException e) { this.rethrow(e, sql, params); } finally { close(stmt); if (closeConn) { close(conn); } if (LogUpdateSql.logInfoOrDebug()) { LogUpdateSql.logFormatTimeNow(startTime, sql, params); } } return rows; }
From source file:org.bidtime.dbutils.QueryRunnerEx.java
/** * Executes the given INSERT SQL statement. * @param conn The connection to use for the query call. * @param closeConn True if the connection should be closed, false otherwise. * @param sql The SQL statement to execute. * @param rsh The handler used to create the result object from * the <code>ResultSet</code> of auto-generated keys. * @param params The query replacement parameters. * @return An object generated by the handler. * @throws SQLException If there are database or parameter errors. * @since 1.6// www . ja v a 2 s . c o m */ private <T> T insert(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException { if (conn == null) { throw new SQLException("Null connection"); } if (sql == null) { if (closeConn) { close(conn); } throw new SQLException("Null SQL statement"); } if (rsh == null) { if (closeConn) { close(conn); } throw new SQLException("Null ResultSetHandler"); } PreparedStatement stmt = null; long startTime = System.currentTimeMillis(); T generatedKeys = null; try { stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setQueryTimeout(StmtParams.getInstance().getStmtUpdateTimeOut()); this.fillStatement(stmt, params); stmt.executeUpdate(); ResultSet resultSet = stmt.getGeneratedKeys(); generatedKeys = rsh.handle(resultSet); } catch (SQLException e) { this.rethrow(e, sql, params); } finally { close(stmt); if (closeConn) { close(conn); } if (LogInsertSql.logInfoOrDebug()) { LogInsertSql.logFormatTimeNow(startTime, sql, params); } } return generatedKeys; }
From source file:org.bidtime.dbutils.QueryRunnerEx.java
/** * Executes the given batch of INSERT SQL statements. * @param conn The connection to use for the query call. * @param closeConn True if the connection should be closed, false otherwise. * @param sql The SQL statement to execute. * @param rsh The handler used to create the result object from * the <code>ResultSet</code> of auto-generated keys. * @param params The query replacement parameters. * @return The result generated by the handler. * @throws SQLException If there are database or parameter errors. * @since 1.6// w w w .j av a2s. c om */ private <T> T insertBatch(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object[][] params) throws SQLException { if (conn == null) { throw new SQLException("Null connection"); } if (sql == null) { if (closeConn) { close(conn); } throw new SQLException("Null SQL statement"); } if (params == null) { if (closeConn) { close(conn); } throw new SQLException("Null parameters. If parameters aren't need, pass an empty array."); } PreparedStatement stmt = null; long startTime = System.currentTimeMillis(); T generatedKeys = null; try { stmt = this.prepareStatement(conn, sql, Statement.RETURN_GENERATED_KEYS); stmt.setQueryTimeout(StmtParams.getInstance().getStmtBatchTimeOut()); for (int i = 0; i < params.length; i++) { this.fillStatement(stmt, params[i]); stmt.addBatch(); } stmt.executeBatch(); ResultSet rs = stmt.getGeneratedKeys(); generatedKeys = rsh.handle(rs); } catch (SQLException e) { this.rethrow(e, sql, (Object[]) params); } finally { close(stmt); if (closeConn) { close(conn); } if (LogInsertSql.logInfoOrDebug()) { LogInsertSql.logFormatTimeNow(startTime, sql, params); } } return generatedKeys; }
From source file:org.guzz.transaction.AbstractTranSessionImpl.java
/** * Apply the current query timeout, if any, to the current <code>PreparedStatement</code>. * /*from ww w .j a va2 s . c om*/ * @param pstm PreparedStatement * @see java.sql.PreparedStatement#setQueryTimeout */ public void applyQueryTimeout(PreparedStatement pstm) { if (hasQueryTimeout()) { try { pstm.setQueryTimeout(getQueryTimeoutInSeconds()); } catch (SQLException e) { throw new JDBCException("failed to setQueryTimeout to :" + getQueryTimeoutInSeconds(), e, e.getSQLState()); } } }
From source file:org.jumpmind.db.sql.JdbcSqlTemplate.java
public <T> T queryForObject(final String sql, final Class<T> clazz, final Object... args) { logSql(sql, args);//from www . jav a 2 s . c o m return execute(new IConnectionCallback<T>() { public T execute(Connection con) throws SQLException { T result = null; PreparedStatement ps = null; ResultSet rs = null; try { ps = con.prepareStatement(expandSql(sql, args)); ps.setQueryTimeout(settings.getQueryTimeout()); setValues(ps, expandArgs(sql, args)); rs = ps.executeQuery(); if (rs.next()) { result = getObjectFromResultSet(rs, clazz); } } finally { close(rs); close(ps); } return result; } }); }
From source file:org.jumpmind.db.sql.JdbcSqlTemplate.java
public byte[] queryForBlob(final String sql, final int jdbcTypeCode, final String jdbcTypeName, final Object... args) { logSql(sql, args);/*from w w w . ja va 2 s . c om*/ return execute(new IConnectionCallback<byte[]>() { public byte[] execute(Connection con) throws SQLException { if (lobHandler.needsAutoCommitFalseForBlob(jdbcTypeCode, jdbcTypeName)) { con.setAutoCommit(false); } byte[] result = null; PreparedStatement ps = null; ResultSet rs = null; try { ps = con.prepareStatement(sql); ps.setQueryTimeout(settings.getQueryTimeout()); setValues(ps, args); rs = ps.executeQuery(); if (rs.next()) { result = lobHandler.getBlobAsBytes(rs, 1, jdbcTypeCode, jdbcTypeName); } } finally { if (lobHandler.needsAutoCommitFalseForBlob(jdbcTypeCode, jdbcTypeName) && con != null) { con.setAutoCommit(true); } close(rs); close(ps); } return result; } }); }
From source file:org.jumpmind.db.sql.JdbcSqlTemplate.java
public String queryForClob(final String sql, final int jdbcTypeCode, final String jdbcTypeName, final Object... args) { logSql(sql, args);//from www .j av a 2 s . c o m return execute(new IConnectionCallback<String>() { public String execute(Connection con) throws SQLException { String result = null; PreparedStatement ps = null; ResultSet rs = null; try { ps = con.prepareStatement(sql); ps.setQueryTimeout(settings.getQueryTimeout()); setValues(ps, args); rs = ps.executeQuery(); if (rs.next()) { result = lobHandler.getClobAsString(rs, 1, jdbcTypeCode, jdbcTypeName); } } finally { close(rs); close(ps); } return result; } }); }