Example usage for java.sql SQLTransientException getMessage

List of usage examples for java.sql SQLTransientException getMessage

Introduction

In this page you can find the example usage for java.sql SQLTransientException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.apache.hadoop.hive.ql.exec.Utilities.java

/**
 * Retry SQL execution with random backoff (same as the one implemented in HDFS-767).
 * This function only retries when the SQL query throws a SQLTransientException (which
 * might be able to succeed with a simple retry). It doesn't retry when the exception
 * is a SQLRecoverableException or SQLNonTransientException. For SQLRecoverableException
 * the caller needs to reconnect to the database and restart the whole transaction.
 *
 * @param cmd the SQL command//from  w ww .  jav a 2 s .c o  m
 * @param stmt the prepared statement of SQL.
 * @param baseWindow  The base time window (in milliseconds) before the next retry.
 * see {@link #getRandomWaitTime} for details.
 * @param maxRetries the maximum # of retries when getting a SQLTransientException.
 * @throws SQLException throws SQLRecoverableException or SQLNonTransientException the
 * first time it is caught, or SQLTransientException when the maxRetries has reached.
 */
public static <T> T executeWithRetry(SQLCommand<T> cmd, PreparedStatement stmt, long baseWindow, int maxRetries)
        throws SQLException {

    Random r = new Random();
    T result = null;

    // retry with # of maxRetries before throwing exception
    for (int failures = 0;; failures++) {
        try {
            result = cmd.run(stmt);
            return result;
        } catch (SQLTransientException e) {
            LOG.warn("Failure and retry #" + failures + " with exception " + e.getMessage());
            if (failures >= maxRetries) {
                throw e;
            }
            long waitTime = getRandomWaitTime(baseWindow, failures, r);
            try {
                Thread.sleep(waitTime);
            } catch (InterruptedException iex) {
            }
        } catch (SQLException e) {
            // throw other types of SQLExceptions (SQLNonTransientException / SQLRecoverableException)
            throw e;
        }
    }
}