Java Code Examples for org.springframework.dao.TransientDataAccessResourceException

The following examples show how to use org.springframework.dao.TransientDataAccessResourceException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
@SuppressWarnings("resource")
public void customErrorCodeTranslation() {
	new ClassPathXmlApplicationContext("test-custom-translators-context.xml",
			CustomSQLExceptionTranslatorRegistrarTests.class);

	SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
	SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
	sext.setSqlErrorCodes(codes);

	DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
	assertNotNull("Should have been translated", exFor4200);
	assertTrue("Should have been instance of BadSqlGrammarException",
		BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass()));

	DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
	assertNotNull("Should have been translated", exFor2);
	assertTrue("Should have been instance of TransientDataAccessResourceException",
		TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass()));

	DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
	assertNull("Should not have been translated", exFor3);
}
 
Example 2
@Test
@SuppressWarnings("resource")
public void customErrorCodeTranslation() {
	new ClassPathXmlApplicationContext("test-custom-translators-context.xml",
			CustomSQLExceptionTranslatorRegistrarTests.class);

	SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
	SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
	sext.setSqlErrorCodes(codes);

	DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
	assertNotNull("Should have been translated", exFor4200);
	assertTrue("Should have been instance of BadSqlGrammarException",
		BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass()));

	DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
	assertNotNull("Should have been translated", exFor2);
	assertTrue("Should have been instance of TransientDataAccessResourceException",
		TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass()));

	DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
	assertNull("Should not have been translated", exFor3);
}
 
Example 3
@Test
@SuppressWarnings("resource")
public void customErrorCodeTranslation() {
	new ClassPathXmlApplicationContext("test-custom-translators-context.xml",
			CustomSQLExceptionTranslatorRegistrarTests.class);

	SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
	SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
	sext.setSqlErrorCodes(codes);

	DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
	assertNotNull("Should have been translated", exFor4200);
	assertTrue("Should have been instance of BadSqlGrammarException",
		BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass()));

	DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
	assertNotNull("Should have been translated", exFor2);
	assertTrue("Should have been instance of TransientDataAccessResourceException",
		TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass()));

	DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
	assertNull("Should not have been translated", exFor3);
}
 
Example 4
@Test
void canRetry_retryPossibleDueToAvailableDatabase_returnsTrue() throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);

	DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(
			amazonRDS, "test");
	when(amazonRDS.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")));

	RetryContext retryContext = policy.open(new RetryContextSupport(null));

	// Act
	policy.registerThrowable(retryContext,
			new TransientDataAccessResourceException("not available"));

	// Assert
	assertThat(policy.canRetry(retryContext)).isTrue();
	policy.close(retryContext);
}
 
Example 5
@Test
void canRetry_retryNotPossibleDueToNoDatabase_returnsFalse() throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);

	DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(
			amazonRDS, "test");

	when(amazonRDS.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenThrow(new DBInstanceNotFoundException("test"));

	RetryContext retryContext = policy.open(new RetryContextSupport(null));

	// Act
	policy.registerThrowable(retryContext,
			new TransientDataAccessResourceException("not available"));

	// Assert
	assertThat(policy.canRetry(retryContext)).isFalse();
	policy.close(retryContext);
}
 
Example 6
@Test
public void testCustomErrorCodeTranslation() {

	SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);

	SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 1);
	DataAccessException daeex = sext.translate("task", "SQL", dataIntegrityViolationEx);
	assertEquals(dataIntegrityViolationEx, daeex.getCause());
	assertTrue(daeex instanceof BadSqlGrammarException);

	SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 2);
	DataAccessException darex = sext.translate("task", "SQL", dataAccessResourceEx);
	assertEquals(dataIntegrityViolationEx, daeex.getCause());
	assertTrue(darex instanceof TransientDataAccessResourceException);

}
 
Example 7
@Test
public void testCustomErrorCodeTranslation() {

	SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
	SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
	sext.setSqlErrorCodes(codes);

	DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
	assertNotNull("Should have been translated", exFor4200);
	assertTrue("Should have been instance of BadSqlGrammarException",
			BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass()));

	DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
	assertNotNull("Should have been translated", exFor2);
	assertTrue("Should have been instance of TransientDataAccessResourceException",
			TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass()));

	DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
	assertNull("Should not have been translated", exFor3);
}
 
Example 8
Source Project: genie   Source File: DataServiceRetryAspect.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructor.
 *
 * @param dataServiceRetryProperties retry properties
 */
public DataServiceRetryAspect(final DataServiceRetryProperties dataServiceRetryProperties) {
    this.retryTemplate = new RetryTemplate();
    this.retryTemplate.setRetryPolicy(
        new SimpleRetryPolicy(
            dataServiceRetryProperties.getNoOfRetries(),
            new ImmutableMap.Builder<Class<? extends Throwable>, Boolean>()
                .put(CannotGetJdbcConnectionException.class, true)
                .put(CannotAcquireLockException.class, true)
                .put(DeadlockLoserDataAccessException.class, true)
                .put(OptimisticLockingFailureException.class, true)
                .put(PessimisticLockingFailureException.class, true)
                .put(ConcurrencyFailureException.class, true)
                // Will this work for cases where the write queries timeout on the client?
                .put(QueryTimeoutException.class, true)
                .put(TransientDataAccessResourceException.class, true)
                .put(JpaSystemException.class, true)
                .build()
        )
    );
    final ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
    backOffPolicy.setInitialInterval(dataServiceRetryProperties.getInitialInterval());
    backOffPolicy.setMaxInterval(dataServiceRetryProperties.getMaxInterval());
    this.retryTemplate.setBackOffPolicy(backOffPolicy);
}
 
Example 9
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {

	if (ex instanceof DataAccessException) {
		return (DataAccessException) ex;
	} else if (ex instanceof DiscoveryException) {
		return translateImpl((Neo4jException) ex, TransientDataAccessResourceException::new);
	} else if (ex instanceof DatabaseException) {
		return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
	} else if (ex instanceof ServiceUnavailableException) {
		return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
	} else if (ex instanceof SessionExpiredException) {
		return translateImpl((Neo4jException) ex, RecoverableDataAccessException::new);
	} else if (ex instanceof ProtocolException) {
		return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
	} else if (ex instanceof TransientException) {
		return translateImpl((Neo4jException) ex, TransientDataAccessResourceException::new);
	} else if (ex instanceof ValueException) {
		return translateImpl((Neo4jException) ex, InvalidDataAccessApiUsageException::new);
	} else if (ex instanceof AuthenticationException) {
		return translateImpl((Neo4jException) ex, PermissionDeniedDataAccessException::new);
	} else if (ex instanceof ResultConsumedException) {
		return translateImpl((Neo4jException) ex, InvalidDataAccessApiUsageException::new);
	} else if (ex instanceof FatalDiscoveryException) {
		return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
	} else if (ex instanceof TransactionNestingException) {
		return translateImpl((Neo4jException) ex, InvalidDataAccessApiUsageException::new);
	} else if (ex instanceof ClientException) {
		return translateImpl((Neo4jException) ex, InvalidDataAccessResourceUsageException::new);
	}

	log.warn(() -> String.format("Don't know how to translate exception of type %s", ex.getClass()));
	return null;
}
 
Example 10
@Override
@Nullable
protected DataAccessException doTranslate(String task, @Nullable String sql, SQLException ex) {
	// First, the getSQLState check...
	String sqlState = getSqlState(ex);
	if (sqlState != null && sqlState.length() >= 2) {
		String classCode = sqlState.substring(0, 2);
		if (logger.isDebugEnabled()) {
			logger.debug("Extracted SQL state class '" + classCode + "' from value '" + sqlState + "'");
		}
		if (BAD_SQL_GRAMMAR_CODES.contains(classCode)) {
			return new BadSqlGrammarException(task, (sql != null ? sql : ""), ex);
		}
		else if (DATA_INTEGRITY_VIOLATION_CODES.contains(classCode)) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (DATA_ACCESS_RESOURCE_FAILURE_CODES.contains(classCode)) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (TRANSIENT_DATA_ACCESS_RESOURCE_CODES.contains(classCode)) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (CONCURRENCY_FAILURE_CODES.contains(classCode)) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
	}

	// For MySQL: exception class name indicating a timeout?
	// (since MySQL doesn't throw the JDBC 4 SQLTimeoutException)
	if (ex.getClass().getName().contains("Timeout")) {
		return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
	}

	// Couldn't resolve anything proper - resort to UncategorizedSQLException.
	return null;
}
 
Example 11
@Test
public void dataAccessResourceException() {
	SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 2);
	DataAccessException dae = sext.translate("task", "SQL", dataAccessResourceEx);
	assertEquals(dataAccessResourceEx, dae.getCause());
	assertThat(dae, instanceOf(TransientDataAccessResourceException.class));
}
 
Example 12
@Override
public DataAccessException translate(String task, @Nullable String sql, SQLException ex) {
	if (ex.getErrorCode() == 2) {
		return new TransientDataAccessResourceException("Custom", ex);
	}
	return null;
}
 
Example 13
@Override
@Nullable
protected DataAccessException doTranslate(String task, @Nullable String sql, SQLException ex) {
	// First, the getSQLState check...
	String sqlState = getSqlState(ex);
	if (sqlState != null && sqlState.length() >= 2) {
		String classCode = sqlState.substring(0, 2);
		if (logger.isDebugEnabled()) {
			logger.debug("Extracted SQL state class '" + classCode + "' from value '" + sqlState + "'");
		}
		if (BAD_SQL_GRAMMAR_CODES.contains(classCode)) {
			return new BadSqlGrammarException(task, (sql != null ? sql : ""), ex);
		}
		else if (DATA_INTEGRITY_VIOLATION_CODES.contains(classCode)) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (DATA_ACCESS_RESOURCE_FAILURE_CODES.contains(classCode)) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (TRANSIENT_DATA_ACCESS_RESOURCE_CODES.contains(classCode)) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (CONCURRENCY_FAILURE_CODES.contains(classCode)) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
	}

	// For MySQL: exception class name indicating a timeout?
	// (since MySQL doesn't throw the JDBC 4 SQLTimeoutException)
	if (ex.getClass().getName().contains("Timeout")) {
		return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
	}

	// Couldn't resolve anything proper - resort to UncategorizedSQLException.
	return null;
}
 
Example 14
@Test
public void dataAccessResourceException() {
	SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 2);
	DataAccessException dae = sext.translate("task", "SQL", dataAccessResourceEx);
	assertEquals(dataAccessResourceEx, dae.getCause());
	assertThat(dae, instanceOf(TransientDataAccessResourceException.class));
}
 
Example 15
@Override
public DataAccessException translate(String task, @Nullable String sql, SQLException ex) {
	if (ex.getErrorCode() == 2) {
		return new TransientDataAccessResourceException("Custom", ex);
	}
	return null;
}
 
Example 16
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
	if (ex instanceof SQLTransientException) {
		if (ex instanceof SQLTransientConnectionException) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTransactionRollbackException) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTimeoutException) {
			return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLNonTransientException) {
		if (ex instanceof SQLNonTransientConnectionException) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLDataException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLIntegrityConstraintViolationException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLInvalidAuthorizationSpecException) {
			return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLSyntaxErrorException) {
			return new BadSqlGrammarException(task, sql, ex);
		}
		else if (ex instanceof SQLFeatureNotSupportedException) {
			return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLRecoverableException) {
		return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
	}

	// Fallback to Spring's own SQL state translation...
	return null;
}
 
Example 17
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
	// First, the getSQLState check...
	String sqlState = getSqlState(ex);
	if (sqlState != null && sqlState.length() >= 2) {
		String classCode = sqlState.substring(0, 2);
		if (logger.isDebugEnabled()) {
			logger.debug("Extracted SQL state class '" + classCode + "' from value '" + sqlState + "'");
		}
		if (BAD_SQL_GRAMMAR_CODES.contains(classCode)) {
			return new BadSqlGrammarException(task, sql, ex);
		}
		else if (DATA_INTEGRITY_VIOLATION_CODES.contains(classCode)) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (DATA_ACCESS_RESOURCE_FAILURE_CODES.contains(classCode)) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (TRANSIENT_DATA_ACCESS_RESOURCE_CODES.contains(classCode)) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (CONCURRENCY_FAILURE_CODES.contains(classCode)) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
	}

	// For MySQL: exception class name indicating a timeout?
	// (since MySQL doesn't throw the JDBC 4 SQLTimeoutException)
	if (ex.getClass().getName().contains("Timeout")) {
		return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
	}

	// Couldn't resolve anything proper - resort to UncategorizedSQLException.
	return null;
}
 
Example 18
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
	if (ex instanceof SQLTransientException) {
		if (ex instanceof SQLTransientConnectionException) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTransactionRollbackException) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTimeoutException) {
			return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLNonTransientException) {
		if (ex instanceof SQLNonTransientConnectionException) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLDataException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLIntegrityConstraintViolationException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLInvalidAuthorizationSpecException) {
			return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLSyntaxErrorException) {
			return new BadSqlGrammarException(task, sql, ex);
		}
		else if (ex instanceof SQLFeatureNotSupportedException) {
			return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLRecoverableException) {
		return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
	}

	// Fallback to Spring's own SQL state translation...
	return null;
}
 
Example 19
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
	// First, the getSQLState check...
	String sqlState = getSqlState(ex);
	if (sqlState != null && sqlState.length() >= 2) {
		String classCode = sqlState.substring(0, 2);
		if (logger.isDebugEnabled()) {
			logger.debug("Extracted SQL state class '" + classCode + "' from value '" + sqlState + "'");
		}
		if (BAD_SQL_GRAMMAR_CODES.contains(classCode)) {
			return new BadSqlGrammarException(task, sql, ex);
		}
		else if (DATA_INTEGRITY_VIOLATION_CODES.contains(classCode)) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (DATA_ACCESS_RESOURCE_FAILURE_CODES.contains(classCode)) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (TRANSIENT_DATA_ACCESS_RESOURCE_CODES.contains(classCode)) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (CONCURRENCY_FAILURE_CODES.contains(classCode)) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
	}

	// For MySQL: exception class name indicating a timeout?
	// (since MySQL doesn't throw the JDBC 4 SQLTimeoutException)
	if (ex.getClass().getName().contains("Timeout")) {
		return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
	}

	// Couldn't resolve anything proper - resort to UncategorizedSQLException.
	return null;
}
 
Example 20
@Test
public void dataAccessResourceException() {
	SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 2);
	DataAccessException dae = sext.translate("task", "SQL", dataAccessResourceEx);
	assertEquals(dataAccessResourceEx, dae.getCause());
	assertThat(dae, instanceOf(TransientDataAccessResourceException.class));
}
 
Example 21
@Override
public DataAccessException translate(String task, String sql, SQLException ex) {
	if (ex.getErrorCode() == 2) {
		return new TransientDataAccessResourceException("Custom", ex);
	}
	return null;
}
 
Example 22
Source Project: spring-cloud-aws   Source File: SqlRetryPolicyTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void testRetryTransientExceptions() throws Exception {
	SqlRetryPolicy sqlRetryPolicy = new SqlRetryPolicy();
	RetryContextSupport retryContext = new RetryContextSupport(null);

	retryContext.registerThrowable(new SQLTransientException("foo"));
	assertThat(sqlRetryPolicy.canRetry(retryContext)).isTrue();

	retryContext.registerThrowable(new TransientDataAccessResourceException("foo"));
	assertThat(sqlRetryPolicy.canRetry(retryContext)).isTrue();
}
 
Example 23
@Test
void canRetry_withResourceIdResolver_returnsTrue() throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);
	ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class);

	DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(
			amazonRDS, "foo");
	when(amazonRDS.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")));

	when(resourceIdResolver.resolveToPhysicalResourceId("foo")).thenReturn("test");

	policy.setResourceIdResolver(resourceIdResolver);

	RetryContext retryContext = policy.open(new RetryContextSupport(null));

	// Act
	policy.registerThrowable(retryContext,
			new TransientDataAccessResourceException("not available"));

	// Assert
	assertThat(policy.canRetry(retryContext)).isTrue();
	policy.close(retryContext);
}
 
Example 24
@Test
void canRetry_multipleDatabasesFoundForInstanceIdentifier_reportsException()
		throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);

	DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(
			amazonRDS, "test");

	DescribeDBInstancesResult describeDBInstancesResult = new DescribeDBInstancesResult()
			.withDBInstances(new DBInstance(), new DBInstance());
	when(amazonRDS.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(describeDBInstancesResult);

	RetryContext retryContext = policy.open(new RetryContextSupport(null));

	// Act
	policy.registerThrowable(retryContext,
			new TransientDataAccessResourceException("not available"));

	// Assert

	assertThatThrownBy(() -> policy.canRetry(retryContext))
			.isInstanceOf(IllegalStateException.class)
			.hasMessageContaining("Multiple databases found for same identifier");
}
 
Example 25
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
	if (ex instanceof SQLTransientException) {
		if (ex instanceof SQLTransientConnectionException) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTransactionRollbackException) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTimeoutException) {
			return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLNonTransientException) {
		if (ex instanceof SQLNonTransientConnectionException) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLDataException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLIntegrityConstraintViolationException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLInvalidAuthorizationSpecException) {
			return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLSyntaxErrorException) {
			return new BadSqlGrammarException(task, sql, ex);
		}
		else if (ex instanceof SQLFeatureNotSupportedException) {
			return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLRecoverableException) {
		return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
	}

	// Fallback to Spring's own SQL state translation...
	return null;
}
 
Example 26
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
	// First, the getSQLState check...
	String sqlState = getSqlState(ex);
	if (sqlState != null && sqlState.length() >= 2) {
		String classCode = sqlState.substring(0, 2);
		if (logger.isDebugEnabled()) {
			logger.debug("Extracted SQL state class '" + classCode + "' from value '" + sqlState + "'");
		}
		if (BAD_SQL_GRAMMAR_CODES.contains(classCode)) {
			return new BadSqlGrammarException(task, sql, ex);
		}
		else if (DATA_INTEGRITY_VIOLATION_CODES.contains(classCode)) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (DATA_ACCESS_RESOURCE_FAILURE_CODES.contains(classCode)) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (TRANSIENT_DATA_ACCESS_RESOURCE_CODES.contains(classCode)) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (CONCURRENCY_FAILURE_CODES.contains(classCode)) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
	}

	// For MySQL: exception class name indicating a timeout?
	// (since MySQL doesn't throw the JDBC 4 SQLTimeoutException)
	if (ex.getClass().getName().contains("Timeout")) {
		return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
	}

	// Couldn't resolve anything proper - resort to UncategorizedSQLException.
	return null;
}
 
Example 27
@Override
public DataAccessException translate(String task, String sql, SQLException ex) {
	if (ex.getErrorCode() == 2) {
		return new TransientDataAccessResourceException("Custom", ex);
	}
	return null;
}
 
Example 28
@Override
@Nullable
protected DataAccessException doTranslate(String task, @Nullable String sql, SQLException ex) {
	if (ex instanceof SQLTransientException) {
		if (ex instanceof SQLTransientConnectionException) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTransactionRollbackException) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTimeoutException) {
			return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLNonTransientException) {
		if (ex instanceof SQLNonTransientConnectionException) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLDataException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLIntegrityConstraintViolationException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLInvalidAuthorizationSpecException) {
			return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLSyntaxErrorException) {
			return new BadSqlGrammarException(task, (sql != null ? sql : ""), ex);
		}
		else if (ex instanceof SQLFeatureNotSupportedException) {
			return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLRecoverableException) {
		return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
	}

	// Fallback to Spring's own SQL state translation...
	return null;
}
 
Example 29
@Test
public void testTranslateTransientDataAccessResourceFailure() throws Exception {
	doTest("S1", TransientDataAccessResourceException.class);
}
 
Example 30
@Test
public void errorCodeTranslation() {
	SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);

	SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 0);
	DataIntegrityViolationException divex = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx);
	assertEquals(dataIntegrityViolationEx, divex.getCause());

	SQLException featureNotSupEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0);
	InvalidDataAccessApiUsageException idaex = (InvalidDataAccessApiUsageException) sext.translate("task", "SQL", featureNotSupEx);
	assertEquals(featureNotSupEx, idaex.getCause());

	SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0);
	DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2);
	assertEquals(dataIntegrityViolationEx2, divex2.getCause());

	SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0);
	PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx);
	assertEquals(permissionDeniedEx, pdaex.getCause());

	SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0);
	DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccessResourceEx);
	assertEquals(dataAccessResourceEx, darex.getCause());

	SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0);
	BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2);
	assertEquals("SQL2", bsgex2.getSql());
	assertEquals(badSqlEx2, bsgex2.getSQLException());

	SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0);
	ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx);
	assertEquals(tranRollbackEx, cfex.getCause());

	SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0);
	TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx);
	assertEquals(transientConnEx, tdarex.getCause());

	SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0);
	QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2);
	assertEquals(transientConnEx2, tdarex2.getCause());

	SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0);
	RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx);
	assertEquals(recoverableEx, rdaex2.getCause());

	// Test classic error code translation. We should move there next if the exception we pass in is not one
	// of the new sub-classes.
	SQLException sexEct = new SQLException("", "", 1);
	BadSqlGrammarException bsgEct = (BadSqlGrammarException) sext.translate("task", "SQL-ECT", sexEct);
	assertEquals("SQL-ECT", bsgEct.getSql());
	assertEquals(sexEct, bsgEct.getSQLException());

	// Test fallback. We assume that no database will ever return this error code,
	// but 07xxx will be bad grammar picked up by the fallback SQLState translator
	SQLException sexFbt = new SQLException("", "07xxx", 666666666);
	BadSqlGrammarException bsgFbt = (BadSqlGrammarException) sext.translate("task", "SQL-FBT", sexFbt);
	assertEquals("SQL-FBT", bsgFbt.getSql());
	assertEquals(sexFbt, bsgFbt.getSQLException());
	// and 08xxx will be data resource failure (non-transient) picked up by the fallback SQLState translator
	SQLException sexFbt2 = new SQLException("", "08xxx", 666666666);
	DataAccessResourceFailureException darfFbt = (DataAccessResourceFailureException) sext.translate("task", "SQL-FBT2", sexFbt2);
	assertEquals(sexFbt2, darfFbt.getCause());
}