use of java.sql.SQLTransientException in project hive by apache.
the class Utilities method executeWithRetry.
/**
* 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
* @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;
}
}
}
use of java.sql.SQLTransientException in project jdk8u_jdk by JetBrains.
the class SQLTransientExceptionTests method test4.
/**
* Create SQLTransientException with message, SQLState, and error code
*/
@Test
public void test4() {
SQLTransientException ex = new SQLTransientException(reason, state, errorCode);
assertTrue(ex.getMessage().equals(reason) && ex.getSQLState().equals(state) && ex.getCause() == null && ex.getErrorCode() == errorCode);
}
use of java.sql.SQLTransientException in project jdk8u_jdk by JetBrains.
the class SQLTransientExceptionTests method test6.
/**
* Create SQLTransientException with message, SQLState, and Throwable
*/
@Test
public void test6() {
SQLTransientException ex = new SQLTransientException(reason, state, t);
assertTrue(ex.getMessage().equals(reason) && ex.getSQLState().equals(state) && cause.equals(ex.getCause().toString()) && ex.getErrorCode() == 0);
}
use of java.sql.SQLTransientException in project jdk8u_jdk by JetBrains.
the class SQLTransientExceptionTests method test.
/**
* Create SQLTransientException and setting all objects to null
*/
@Test
public void test() {
SQLTransientException e = new SQLTransientException(null, null, errorCode, null);
assertTrue(e.getMessage() == null && e.getSQLState() == null && e.getCause() == null && e.getErrorCode() == errorCode);
}
use of java.sql.SQLTransientException in project jdk8u_jdk by JetBrains.
the class SQLTransientExceptionTests method test7.
/**
* Create SQLTransientException with message, and Throwable
*/
@Test
public void test7() {
SQLTransientException ex = new SQLTransientException(reason, t);
assertTrue(ex.getMessage().equals(reason) && ex.getSQLState() == null && cause.equals(ex.getCause().toString()) && ex.getErrorCode() == 0);
}
Aggregations