微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javax.persistence.PessimisticLockException的实例源码

项目:OpenCyclos    文件ExceptionHelper.java   
private static boolean isLockingException(final Throwable t,final boolean recurse) {
    if (t instanceof LockingException || t instanceof pessimisticLockException) {
        return true;
    }
    if (t instanceof sqlException) {
        final sqlException e = (sqlException) t;
        return e.getErrorCode() == ER_LOCK_WAIT_TIMEOUT || ST_LOCK.equals(e.getsqlState());
    }
    if (recurse) {
        for (final Throwable thr : ExceptionUtils.getThrowables(t)) {
            if (isLockingException(thr,false)) {
                return true;
            }
        }
    }
    return false;
}
项目:Jouve-Project    文件RecordServicesImpl.java   
@Override
public int count(ConnectorInstance connectorInstance) {
    int attempts = 0;
    while (true) {
        try {
            String ql = "SELECT COUNT(r.id) FROM Record r WHERE r.connectorInstance = :connectorInstance";
            Query query = this.getEntityManager().createquery(ql);
            query.setParameter("connectorInstance",connectorInstance);
            return ((Number) query.getSingleResult()).intValue();
        } catch (pessimisticLockException e) {
            attempts++;
            if (attempts > 100) {
                throw e;
            }
        }
    }
}
项目:Camel    文件JpaConsumer.java   
/**
 * A strategy method to lock an object with an exclusive lock so that it can
 * be processed
 * 
 * @param entity the entity to be locked
 * @param entityManager entity manager
 * @return true if the entity was locked
 */
protected boolean lockEntity(Object entity,EntityManager entityManager) {
    if (!getEndpoint().isConsumeLockEntity()) {
        return true;
    }
    try {
        LOG.debug("Acquiring exclusive lock on entity: {}",entity);
        if (isSkipLockedEntity()) {
            entityManager.lock(entity,lockModeType,NowAIT);
        } else {
            entityManager.lock(entity,lockModeType);
        }
        return true;
    } catch (Exception e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Failed to achieve lock on entity: " + entity + ". Reason: " + e,e);
        }
        if (e instanceof pessimisticLockException || e instanceof OptimisticLockException) {
            //transaction marked as rollback can't continue gracefully
            throw (PersistenceException) e;
        }
        //Todo: Find if possible an alternative way to handle results of native queries.
        //Result of native queries are Arrays and cannot be locked by all JPA Providers.
        if (entity.getClass().isArray()) {
            return true;
        }
        return false;
    }
}
项目:Jouve-Project    文件RecordServicesImpl.java   
private int count(RecordCollection collection,Boolean updateIndex,Boolean deleted) {
    int attempts = 0;
    while (true) {
        try {
            String ql = "SELECT COUNT(r.id) FROM Record r WHERE r.connectorInstance.recordCollection = :recordCollection";
            if (Boolean.TRUE.equals(updateIndex)) {
                ql += " AND updateIndex = :updateIndex AND (excluded = false OR excluded = null)";
            }
            if (Boolean.TRUE.equals(deleted)) {
                ql += " AND deleted = :deleted";
            }
            Query query = this.getEntityManager().createquery(ql);
            query.setParameter("recordCollection",collection);
            if (updateIndex != null && Boolean.TRUE.equals(updateIndex)) {
                query.setParameter("updateIndex",updateIndex);
            }
            if (deleted != null && Boolean.TRUE.equals(deleted)) {
                query.setParameter("deleted",deleted);
            }
            return ((Number) query.getSingleResult()).intValue();
        } catch (pessimisticLockException e) {
            attempts++;
            if (attempts > 100) {
                throw e;
            }
        }
    }
}
项目: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(),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;
}
项目:spring4-understanding    文件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,so we return null to indicate
    // that translation should not occur.
    return null;
}
项目:Jouve-Project    文件RecordServicesImpl.java   
@Override
public void deleteAutomaticRecordTags(RecordCollection collection,Date newStartTaggingDate) {
    int attempts = 0;
    while (true) {
        try {
            String sqlTag;
            if (newStartTaggingDate == null) {
                sqlTag = "DELETE FROM RecordTag WHERE manual=? AND record_id IN"
                    + " (SELECT r.id FROM Record r,ConnectorInstance ci,RecordCollection rc"
                    + " WHERE r.connectorInstance_id=ci.id AND ci.recordCollection_id=rc.id AND rc.id=?)";
            } else {
                sqlTag = "DELETE FROM RecordTag WHERE manual=? AND record_id IN"
                    + " (SELECT r.id FROM Record r,RecordCollection rc "
                    + "WHERE r.connectorInstance_id=ci.id AND ci.recordCollection_id=rc.id AND rc.id=? AND"
                    + " (r.lastAutomaticTagging > ? OR r.lastAutomaticTagging IS NULL))";
            }
            Query tagQuery = getEntityManager().createNativeQuery(sqlTag);
            tagQuery.setParameter(1,Boolean.FALSE);
            tagQuery.setParameter(2,collection.getId());
            if (newStartTaggingDate != null) {
                tagQuery.setParameter(3,newStartTaggingDate);
            }
            tagQuery.executeUpdate();

            String sqlRecord;
            if (newStartTaggingDate == null) {
                sqlRecord = "UPDATE Record r SET r.lastAutomaticTagging = null WHERE connectorInstance_id IN"
                    + " (SELECT ci.id FROM ConnectorInstance ci,RecordCollection rc WHERE ci.recordCollection_id=rc.id AND rc.id=?)";
            } else {
                sqlRecord = "UPDATE Record r SET r.lastAutomaticTagging = null WHERE connectorInstance_id IN"
                    + " (SELECT ci.id FROM ConnectorInstance ci,RecordCollection rc WHERE ci.recordCollection_id=rc.id AND"
                    + " rc.id=?) AND (r.lastAutomaticTagging > ? OR r.lastAutomaticTagging IS NULL)";
            }
            Query recordQuery = getEntityManager().createNativeQuery(sqlRecord);
            recordQuery.setParameter(1,collection.getId());
            if (newStartTaggingDate != null) {
                recordQuery.setParameter(2,newStartTaggingDate);
            }
            recordQuery.executeUpdate();
            break;
        } catch (pessimisticLockException e) {
            attempts++;
            if (attempts > 100) {
                throw e;
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。