Example usage for javax.persistence Query setLockMode

List of usage examples for javax.persistence Query setLockMode

Introduction

In this page you can find the example usage for javax.persistence Query setLockMode.

Prototype

Query setLockMode(LockModeType lockMode);

Source Link

Document

Set the lock mode type to be used for the query execution.

Usage

From source file:com.pinterest.rocksplicator.controller.mysql.MySQLTaskQueue.java

@Override
public Task dequeueTask(final String worker) {
    beginTransaction();/*w ww.j a va  2  s. c  o  m*/
    Query query = getEntityManager().createNamedQuery("task.peekDequeue").setMaxResults(1);
    query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    List<TaskEntity> resultList = query.getResultList();
    if (resultList.isEmpty()) {
        LOG.info("No pending task to be dequeud");
        getEntityManager().getTransaction().rollback();
        return null;
    }
    TaskEntity claimedTask = resultList.get(0);
    claimedTask.setState(TaskState.RUNNING.intValue());
    claimedTask.setLastAliveAt(new Date());
    claimedTask.setClaimedWorker(worker);

    getEntityManager().persist(claimedTask);
    claimedTask.getCluster().setLocks(1);
    getEntityManager().persist(claimedTask.getCluster());
    getEntityManager().getTransaction().commit();
    return convertTaskEntityToTask(claimedTask);
}

From source file:com.pinterest.rocksplicator.controller.mysql.MySQLTaskQueue.java

private Cluster ackTask(final long id, final String output, TaskState ackState, boolean unlockCluster) {
    Query query = getEntityManager().createNamedQuery("task.findRunning").setParameter("id", id);
    query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    List<TaskEntity> resultList = query.getResultList();
    if (resultList.isEmpty()) {
        LOG.info("No matching task to ack: {}", id);
        getEntityManager().getTransaction().rollback();
        return null;
    }// w w w .  ja v  a  2s. c o  m
    TaskEntity taskEntity = resultList.get(0);

    taskEntity.setState(ackState.intValue());
    taskEntity.setOutput(output);
    getEntityManager().persist(taskEntity);
    TagEntity cluster = taskEntity.getCluster();
    if (unlockCluster) {
        getEntityManager().lock(cluster, LockModeType.PESSIMISTIC_WRITE);
        cluster.setLocks(0);
        getEntityManager().persist(cluster);
    }
    return new Cluster(cluster.getNamespace(), cluster.getName());
}

From source file:de.micromata.genome.jpa.Emgr.java

/**
 * Set the the query to use select for update.
 *
 * @param query the query//from w ww  .j  av  a2 s.co m
 * @param lockTimetimeInMs the lock timetime in ms
 */
@Override
public void setSelectForUpdate(Query query, int lockTimetimeInMs) {
    query.setHint(AvailableSettings.LOCK_TIMEOUT, lockTimetimeInMs);
    setQueryTimeout(query, lockTimetimeInMs);
    query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
}

From source file:com.mothsoft.alexis.dao.DocumentDaoImpl.java

public List<Document> listTopDocuments(Long userId, Date startDate, Date endDate, int count) {
    final StopWatch stopWatch = new StopWatch();
    stopWatch.start();//from   ww w.j  a  v a  2  s .  com

    final Query query = this.em
            .createQuery("select d from Topic topic join topic.topicDocuments td join td.document d "
                    + "   where topic.userId = :userId "
                    + "     and td.creationDate > :startDate and td.creationDate < :endDate "
                    + "     and td.score > 0.2                                            "
                    + "     order by td.score desc");
    query.setParameter("userId", userId);
    query.setParameter("startDate", startDate);
    query.setParameter("endDate", endDate);
    query.setFirstResult(0);
    query.setMaxResults(count);

    query.setLockMode(LockModeType.NONE);

    @SuppressWarnings("unchecked")
    final List<Document> range = query.getResultList();

    stopWatch.stop();
    logger.debug(stopWatch.toString());

    return range;
}

From source file:org.easy.criteria.QueryProperties.java

public void applyProperties(Query query) {
    if (flashMode != null) {
        log.debug("flashMode = " + flashMode);
        query.setFlushMode(flashMode);// ww  w. j a v  a  2s  .c  o m
    }

    if (lockMode != null) {
        log.debug("lockMode = " + lockMode);
        query.setLockMode(lockMode);
    }

    if (hintKey != null) {
        log.debug("hintKey = " + hintKey);
        log.debug("hintValue = " + hintValue);
        query.setHint(hintKey, hintValue);
    }

    if (startIndex >= 0 && maxResult > 0) {
        log.debug("startIndex = " + startIndex * maxResult);
        query.setFirstResult(startIndex * maxResult);
    }

    if (maxResult > 0) {
        log.debug("maxResult = " + maxResult);
        query.setMaxResults(maxResult);
    }
}