Example usage for javax.persistence TypedQuery setLockMode

List of usage examples for javax.persistence TypedQuery setLockMode

Introduction

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

Prototype

TypedQuery<X> setLockMode(LockModeType lockMode);

Source Link

Document

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

Usage

From source file:eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryReferenceCheckImpl.java

@Override
public void perform(long now) {
    logger.debug("{}", name);
    EntityManager em = datastore.getEntityManager();
    try {/*from  w  ww .jav a2  s.com*/
        em.getTransaction().begin();
        TypedQuery<VirtualCollection> q = em.createNamedQuery("VirtualCollection.findAllPublic",
                VirtualCollection.class);
        q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
        for (VirtualCollection vc : q.getResultList()) {
            checkValidityOfReferences(vc);
        }
        em.getTransaction().commit();
    } catch (RuntimeException e) {
        logger.error("unexpected error while doing " + name, e);
    } finally {
        datastore.closeEntityManager();
    }
}

From source file:eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryMaintenanceImpl.java

protected void handleCollectionsInError(EntityManager em, final Date nowDateError) {
    em.getTransaction().begin();//from w  w  w  .jav  a 2  s.c o m
    TypedQuery<VirtualCollection> q = em.createNamedQuery("VirtualCollection.findAllByState",
            VirtualCollection.class);
    q.setParameter("state", VirtualCollection.State.ERROR);
    q.setParameter("date", nowDateError);
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    for (VirtualCollection vc : q.getResultList()) {
        VirtualCollection.State currentState = vc.getState();
        logger.info("Found [{}] in error state.", vc.getName(), currentState);
        //TODO: handle virtual collections in error state
    }
    em.getTransaction().commit();
}

From source file:eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryMaintenanceImpl.java

protected void purgeDeletedCollections(EntityManager em, final Date nowDatePurge) {
    em.getTransaction().begin();//  w w  w  . j av  a  2 s  .  co m
    TypedQuery<VirtualCollection> q = em.createNamedQuery("VirtualCollection.findAllByState",
            VirtualCollection.class);
    q.setParameter("state", VirtualCollection.State.DELETED);
    q.setParameter("date", nowDatePurge);
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    for (VirtualCollection vc : q.getResultList()) {
        vc.setState(VirtualCollection.State.DEAD);
        em.remove(vc);
        logger.debug("purged virtual collection (id={})", vc.getId());
    }
    em.getTransaction().commit();
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
public Boolean updateMessageForLock(final Message msg) {
    Assert.notNull(msg, "the msg must not be null");

    // acquire pessimistic lock firstly
    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.msgId = :msgId"
            + "     AND (m.state = '" + MsgStateEnum.PARTLY_FAILED + "' " + "     OR m.state = '"
            + MsgStateEnum.POSTPONED + "')";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("msgId", msg.getMsgId());
    // note: https://blogs.oracle.com/carolmcdonald/entry/jpa_2_0_concurrency_and
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    Message dbMsg = q.getSingleResult();

    if (dbMsg != null) {
        // change message's state to PROCESSING
        msg.setState(MsgStateEnum.PROCESSING);
        Date currDate = new Date();
        msg.setStartProcessTimestamp(currDate);
        msg.setLastUpdateTimestamp(currDate);

        update(msg);/*from   w w  w .ja  v a 2  s  . c om*/
    }

    return true;
}

From source file:eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryMaintenanceImpl.java

protected void allocatePersistentIdentifiers(EntityManager em, final Date nowDateAlloc) {
    em.getTransaction().begin();//from w w  w.  java 2 s.  c  o  m
    TypedQuery<VirtualCollection> q = em.createNamedQuery("VirtualCollection.findAllByStates",
            VirtualCollection.class);
    List<VirtualCollection.State> states = new LinkedList<>();
    states.add(VirtualCollection.State.PUBLIC_PENDING);
    states.add(VirtualCollection.State.PUBLIC_FROZEN_PENDING);
    q.setParameter("states", states);
    q.setParameter("date", nowDateAlloc);
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    for (VirtualCollection vc : q.getResultList()) {
        allocatePersistentIdentifier(em, vc);
    }
    em.getTransaction().commit();
}

From source file:com.luna.common.repository.support.SimpleBaseRepository.java

private TypedQuery<M> applyLockMode(TypedQuery<M> query) {
    LockModeType type = crudMethodMetadata.getLockModeType();
    return type == null ? query : query.setLockMode(type);
}

From source file:com.sishuok.es.common.repository.support.SimpleBaseRepository.java

private TypedQuery<M> applyLockMode(TypedQuery<M> query) {
    LockModeType type = lockMetadataProvider == null ? null : lockMetadataProvider.getLockModeType();
    return type == null ? query : query.setLockMode(type);
}

From source file:cn.guoyukun.spring.jpa.repository.support.SimpleBaseRepository.java

private TypedQuery<M> applyRepositoryMethodMetadata(TypedQuery<M> query) {

    if (getRepositoryMethodMetadata() == null) {
        return query;
    }/* w  w  w  .j a  v a 2  s.c  o m*/

    LockModeType type = getRepositoryMethodMetadata().getLockModeType();
    TypedQuery<M> toReturn = type == null ? query : query.setLockMode(type);

    for (Entry<String, Object> hint : getRepositoryMethodMetadata().getQueryHints().entrySet()) {
        query.setHint(hint.getKey(), hint.getValue());
    }

    return Jpa21Utils.tryConfigureFetchGraph(em, toReturn, getRepositoryMethodMetadata().getEntityGraph());
}

From source file:com.uni.dao.etc.UniJpaRepository.java

private TypedQuery<T> applyLockMode(TypedQuery<T> query) {

    LockModeType type = lockMetadataProvider == null ? null : lockMetadataProvider.getLockModeType();
    return type == null ? query : query.setLockMode(type);
}