Search in sources :

Example 1 with SQLTransientException

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;
        }
    }
}
Also used : Random(java.util.Random) SQLException(java.sql.SQLException) SQLTransientException(java.sql.SQLTransientException)

Example 2 with SQLTransientException

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);
}
Also used : SQLTransientException(java.sql.SQLTransientException) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Example 3 with SQLTransientException

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);
}
Also used : SQLTransientException(java.sql.SQLTransientException) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Example 4 with SQLTransientException

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);
}
Also used : SQLTransientException(java.sql.SQLTransientException) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Example 5 with SQLTransientException

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);
}
Also used : SQLTransientException(java.sql.SQLTransientException) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Aggregations

SQLTransientException (java.sql.SQLTransientException)21 Test (org.testng.annotations.Test)16 BaseTest (util.BaseTest)16 SQLException (java.sql.SQLException)6 GeodeticAuthorityFactory (org.apache.sis.referencing.factory.GeodeticAuthorityFactory)2 AuthorityFactory (org.opengis.referencing.AuthorityFactory)2 CRSAuthorityFactory (org.opengis.referencing.crs.CRSAuthorityFactory)2 CSAuthorityFactory (org.opengis.referencing.cs.CSAuthorityFactory)2 DatumAuthorityFactory (org.opengis.referencing.datum.DatumAuthorityFactory)2 CoordinateOperationAuthorityFactory (org.opengis.referencing.operation.CoordinateOperationAuthorityFactory)2 Duration (io.airlift.units.Duration)1 SQLNonTransientException (java.sql.SQLNonTransientException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 SQLTransactionRollbackException (java.sql.SQLTransactionRollbackException)1 SQLTransientConnectionException (java.sql.SQLTransientConnectionException)1 Random (java.util.Random)1 FieldValue (org.firebirdsql.gds.ng.fields.FieldValue)1 RowDescriptor (org.firebirdsql.gds.ng.fields.RowDescriptor)1 Handle (org.skife.jdbi.v2.Handle)1 UnableToObtainConnectionException (org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException)1