org.springframework.dao.PessimisticLockingFailureException Java Examples

The following examples show how to use org.springframework.dao.PessimisticLockingFailureException. 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
Source File: CustomRedisCacheWriter.java    From spring-microservice-exam with MIT License 6 votes vote down vote up
private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection connection) {

        if (!isLockingCacheWriter()) {
            return;
        }

        try {

            while (doCheckLock(name, connection)) {
                Thread.sleep(sleepTime.toMillis());
            }
        } catch (InterruptedException ex) {

            // Re-interrupt current thread, to allow other participants to react.
            Thread.currentThread().interrupt();

            throw new PessimisticLockingFailureException(String.format("Interrupted while waiting to unlock cache %s", name),
                    ex);
        }
    }
 
Example #2
Source File: DefaultRedisCacheWriter.java    From black-shop with Apache License 2.0 6 votes vote down vote up
private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection connection) {

		if (!isLockingCacheWriter()) {
			return;
		}

		try {

			while (doCheckLock(name, connection)) {
				Thread.sleep(sleepTime.toMillis());
			}
		} catch (InterruptedException ex) {

			// Re-interrupt current thread, to allow other participants to react.
			Thread.currentThread().interrupt();

			throw new PessimisticLockingFailureException(String.format("Interrupted while waiting to unlock cache %s", name),
					ex);
		}
	}
 
Example #3
Source File: DefaultRedisCacheWriter.java    From gateway-helper with Apache License 2.0 6 votes vote down vote up
private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection connection) {

        if (!isLockingCacheWriter()) {
            return;
        }

        try {

            while (doCheckLock(name, connection)) {
                Thread.sleep(sleepTime.toMillis());
            }
        } catch (InterruptedException ex) {

            // Re-interrupt current thread, to allow other participants to react.
            Thread.currentThread().interrupt();

            throw new PessimisticLockingFailureException(String.format("Interrupted while waiting to unlock cache %s", name),
                    ex);
        }
    }
 
Example #4
Source File: DefaultRedisCacheWriter.java    From api-gateway-old with Apache License 2.0 6 votes vote down vote up
private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection connection) {

        if (!isLockingCacheWriter()) {
            return;
        }

        try {

            while (doCheckLock(name, connection)) {
                Thread.sleep(sleepTime.toMillis());
            }
        } catch (InterruptedException ex) {

            // Re-interrupt current thread, to allow other participants to react.
            Thread.currentThread().interrupt();

            throw new PessimisticLockingFailureException(String.format("Interrupted while waiting to unlock cache %s", name),
                    ex);
        }
    }
 
Example #5
Source File: DataServiceRetryAspect.java    From genie with 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 #6
Source File: ContentRestExceptionHandler.java    From spring-content with Apache License 2.0 5 votes vote down vote up
/**
* Send a {@code 409 Conflict} in case of concurrent modification.
*
   * @param e the exception to handle.
* @return
   */
  @ExceptionHandler({ LockOwnerException.class,
                      OptimisticLockException.class,
                      OptimisticLockingFailureException.class,
                      PessimisticLockException.class,
                      PessimisticLockingFailureException.class})
  ResponseEntity<ExceptionMessage> handleConflict(Exception e) {
      return errorResponse(HttpStatus.CONFLICT, new HttpHeaders(), e);
  }
 
Example #7
Source File: AbstractRestExceptionHandler.java    From kaif with Apache License 2.0 5 votes vote down vote up
@ExceptionHandler({ PessimisticLockingFailureException.class })
@ResponseBody
public ResponseEntity<E> handlePessimisticLockingFailureException(final PessimisticLockingFailureException ex,
    final WebRequest request) {
  final HttpStatus status = HttpStatus.LOCKED;
  final E errorResponse = createErrorResponse(status,
      i18n(request, "rest-error.PessimisticLockingFailureException"));
  logException(ex, errorResponse, request);
  return new ResponseEntity<>(errorResponse, status);
}
 
Example #8
Source File: EntityManagerFactoryUtils.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Convert the given runtime exception to an appropriate exception from the
 * {@code org.springframework.dao} hierarchy.
 * Return null if no translation is appropriate: any other exception may
 * have resulted from user code, and should not be translated.
 * <p>The most important cases like object not found or optimistic locking failure
 * are covered here. For more fine-granular conversion, JpaTransactionManager etc
 * support sophisticated translation of exceptions via a JpaDialect.
 * @param ex runtime exception that occurred
 * @return the corresponding DataAccessException instance,
 * or {@code null} if the exception should not be translated
 */
@Nullable
public static DataAccessException convertJpaAccessExceptionIfPossible(RuntimeException ex) {
	// Following the JPA specification, a persistence provider can also
	// throw these two exceptions, besides PersistenceException.
	if (ex instanceof IllegalStateException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}
	if (ex instanceof IllegalArgumentException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}

	// Check for well-known PersistenceException subclasses.
	if (ex instanceof EntityNotFoundException) {
		return new JpaObjectRetrievalFailureException((EntityNotFoundException) ex);
	}
	if (ex instanceof NoResultException) {
		return new EmptyResultDataAccessException(ex.getMessage(), 1, ex);
	}
	if (ex instanceof NonUniqueResultException) {
		return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
	}
	if (ex instanceof QueryTimeoutException) {
		return new org.springframework.dao.QueryTimeoutException(ex.getMessage(), ex);
	}
	if (ex instanceof LockTimeoutException) {
		return new CannotAcquireLockException(ex.getMessage(), ex);
	}
	if (ex instanceof PessimisticLockException) {
		return new PessimisticLockingFailureException(ex.getMessage(), ex);
	}
	if (ex instanceof OptimisticLockException) {
		return new JpaOptimisticLockingFailureException((OptimisticLockException) ex);
	}
	if (ex instanceof EntityExistsException) {
		return new DataIntegrityViolationException(ex.getMessage(), ex);
	}
	if (ex instanceof TransactionRequiredException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}

	// If we have another kind of PersistenceException, throw it.
	if (ex instanceof PersistenceException) {
		return new JpaSystemException(ex);
	}

	// If we get here, we have an exception that resulted from user code,
	// rather than the persistence provider, so we return null to indicate
	// that translation should not occur.
	return null;
}
 
Example #9
Source File: EntityManagerFactoryUtils.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Convert the given runtime exception to an appropriate exception from the
 * {@code org.springframework.dao} hierarchy.
 * Return null if no translation is appropriate: any other exception may
 * have resulted from user code, and should not be translated.
 * <p>The most important cases like object not found or optimistic locking failure
 * are covered here. For more fine-granular conversion, JpaTransactionManager etc
 * support sophisticated translation of exceptions via a JpaDialect.
 * @param ex runtime exception that occurred
 * @return the corresponding DataAccessException instance,
 * or {@code null} if the exception should not be translated
 */
@Nullable
public static DataAccessException convertJpaAccessExceptionIfPossible(RuntimeException ex) {
	// Following the JPA specification, a persistence provider can also
	// throw these two exceptions, besides PersistenceException.
	if (ex instanceof IllegalStateException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}
	if (ex instanceof IllegalArgumentException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}

	// Check for well-known PersistenceException subclasses.
	if (ex instanceof EntityNotFoundException) {
		return new JpaObjectRetrievalFailureException((EntityNotFoundException) ex);
	}
	if (ex instanceof NoResultException) {
		return new EmptyResultDataAccessException(ex.getMessage(), 1, ex);
	}
	if (ex instanceof NonUniqueResultException) {
		return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
	}
	if (ex instanceof QueryTimeoutException) {
		return new org.springframework.dao.QueryTimeoutException(ex.getMessage(), ex);
	}
	if (ex instanceof LockTimeoutException) {
		return new CannotAcquireLockException(ex.getMessage(), ex);
	}
	if (ex instanceof PessimisticLockException) {
		return new PessimisticLockingFailureException(ex.getMessage(), ex);
	}
	if (ex instanceof OptimisticLockException) {
		return new JpaOptimisticLockingFailureException((OptimisticLockException) ex);
	}
	if (ex instanceof EntityExistsException) {
		return new DataIntegrityViolationException(ex.getMessage(), ex);
	}
	if (ex instanceof TransactionRequiredException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}

	// If we have another kind of PersistenceException, throw it.
	if (ex instanceof PersistenceException) {
		return new JpaSystemException(ex);
	}

	// If we get here, we have an exception that resulted from user code,
	// rather than the persistence provider, so we return null to indicate
	// that translation should not occur.
	return null;
}
 
Example #10
Source File: EntityManagerFactoryUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Convert the given runtime exception to an appropriate exception from the
 * {@code org.springframework.dao} hierarchy.
 * Return null if no translation is appropriate: any other exception may
 * have resulted from user code, and should not be translated.
 * <p>The most important cases like object not found or optimistic locking failure
 * are covered here. For more fine-granular conversion, JpaTransactionManager etc
 * support sophisticated translation of exceptions via a JpaDialect.
 * @param ex runtime exception that occurred
 * @return the corresponding DataAccessException instance,
 * or {@code null} if the exception should not be translated
 */
public static DataAccessException convertJpaAccessExceptionIfPossible(RuntimeException ex) {
	// Following the JPA specification, a persistence provider can also
	// throw these two exceptions, besides PersistenceException.
	if (ex instanceof IllegalStateException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}
	if (ex instanceof IllegalArgumentException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}

	// Check for well-known PersistenceException subclasses.
	if (ex instanceof EntityNotFoundException) {
		return new JpaObjectRetrievalFailureException((EntityNotFoundException) ex);
	}
	if (ex instanceof NoResultException) {
		return new EmptyResultDataAccessException(ex.getMessage(), 1, ex);
	}
	if (ex instanceof NonUniqueResultException) {
		return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
	}
	if (ex instanceof QueryTimeoutException) {
		return new org.springframework.dao.QueryTimeoutException(ex.getMessage(), ex);
	}
	if (ex instanceof LockTimeoutException) {
		return new CannotAcquireLockException(ex.getMessage(), ex);
	}
	if (ex instanceof PessimisticLockException) {
		return new PessimisticLockingFailureException(ex.getMessage(), ex);
	}
	if (ex instanceof OptimisticLockException) {
		return new JpaOptimisticLockingFailureException((OptimisticLockException) ex);
	}
	if (ex instanceof EntityExistsException) {
		return new DataIntegrityViolationException(ex.getMessage(), ex);
	}
	if (ex instanceof TransactionRequiredException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}

	// If we have another kind of PersistenceException, throw it.
	if (ex instanceof PersistenceException) {
		return new JpaSystemException(ex);
	}

	// If we get here, we have an exception that resulted from user code,
	// rather than the persistence provider, so we return null to indicate
	// that translation should not occur.
	return null;
}
 
Example #11
Source File: EntityManagerFactoryUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Convert the given runtime exception to an appropriate exception from the
 * {@code org.springframework.dao} hierarchy.
 * Return null if no translation is appropriate: any other exception may
 * have resulted from user code, and should not be translated.
 * <p>The most important cases like object not found or optimistic locking failure
 * are covered here. For more fine-granular conversion, JpaTransactionManager etc
 * support sophisticated translation of exceptions via a JpaDialect.
 * @param ex runtime exception that occurred
 * @return the corresponding DataAccessException instance,
 * or {@code null} if the exception should not be translated
 */
public static DataAccessException convertJpaAccessExceptionIfPossible(RuntimeException ex) {
	// Following the JPA specification, a persistence provider can also
	// throw these two exceptions, besides PersistenceException.
	if (ex instanceof IllegalStateException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}
	if (ex instanceof IllegalArgumentException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}

	// Check for well-known PersistenceException subclasses.
	if (ex instanceof EntityNotFoundException) {
		return new JpaObjectRetrievalFailureException((EntityNotFoundException) ex);
	}
	if (ex instanceof NoResultException) {
		return new EmptyResultDataAccessException(ex.getMessage(), 1, ex);
	}
	if (ex instanceof NonUniqueResultException) {
		return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
	}
	if (ex instanceof QueryTimeoutException) {
		return new org.springframework.dao.QueryTimeoutException(ex.getMessage(), ex);
	}
	if (ex instanceof LockTimeoutException) {
		return new CannotAcquireLockException(ex.getMessage(), ex);
	}
	if (ex instanceof PessimisticLockException) {
		return new PessimisticLockingFailureException(ex.getMessage(), ex);
	}
	if (ex instanceof OptimisticLockException) {
		return new JpaOptimisticLockingFailureException((OptimisticLockException) ex);
	}
	if (ex instanceof EntityExistsException) {
		return new DataIntegrityViolationException(ex.getMessage(), ex);
	}
	if (ex instanceof TransactionRequiredException) {
		return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
	}

	// If we have another kind of PersistenceException, throw it.
	if (ex instanceof PersistenceException) {
		return new JpaSystemException((PersistenceException) ex);
	}

	// If we get here, we have an exception that resulted from user code,
	// rather than the persistence provider, so we return null to indicate
	// that translation should not occur.
	return null;
}