类javax.persistence.PessimisticLockException源码实例Demo

下面列出了怎么用javax.persistence.PessimisticLockException的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: cuba   文件: DbBasedCoordinator.java
protected synchronized List<ScheduledTask> getTasks() {
    log.trace("Read all active tasks from DB and lock them");
    EntityManager em = persistence.getEntityManager();
    try {
        //noinspection unchecked
        return em.createQuery("select t from sys$ScheduledTask t where t.active = true")
                .setLockMode(LockModeType.PESSIMISTIC_WRITE)
                .getResultList();
    } catch (PessimisticLockException e) {
        log.info("Unable to acquire lock on tasks");
        if (log.isTraceEnabled()) {
            log.trace("Unable to acquire lock on tasks. Error:", e);
        }
        throw new SchedulingLockException("Lock exception while acquiring tasks");
    }
}
 
/**
* 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);
  }
 
/**
 * 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;
}
 
/**
 * 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;
}
 
源代码5 项目: lams   文件: EntityManagerFactoryUtils.java
/**
 * 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;
}
 
/**
 * 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;
}
 
 类所在包
 同包方法