Example usage for javax.persistence LockModeType PESSIMISTIC_READ

List of usage examples for javax.persistence LockModeType PESSIMISTIC_READ

Introduction

In this page you can find the example usage for javax.persistence LockModeType PESSIMISTIC_READ.

Prototype

LockModeType PESSIMISTIC_READ

To view the source code for javax.persistence LockModeType PESSIMISTIC_READ.

Click Source Link

Document

Pessimistic read lock.

Usage

From source file:com.enioka.jqm.api.HibernateClient.java

@Override
public void killJob(int idJob) {
    // First try to cancel the JI (works if it is not already running)
    try {/* w w w.j a v a  2 s .co m*/
        cancelJob(idJob);
        return;
    } catch (JqmClientException e) {
        // Nothing to do - this is thrown if already running. Just go on, this is a standard kill.
    }

    EntityManager em = null;
    try {
        em = getEm();
        em.getTransaction().begin();
        JobInstance j = em.find(JobInstance.class, idJob, LockModeType.PESSIMISTIC_READ);
        if (j == null) {
            throw new NoResultException("Job instance does not exist or has already finished");
        }
        jqmlogger.trace("The " + j.getState() + " job (ID: " + idJob + ")" + " will be marked for kill");

        j.setState(State.KILLED);

        Message m = new Message();
        m.setJi(idJob);
        m.setTextMessage("Kill attempt on the job");
        em.persist(m);
        em.getTransaction().commit();
    } catch (NoResultException e) {
        throw new JqmInvalidRequestException("An attempt was made to kill a job instance that did not exist.");
    } catch (Exception e) {
        throw new JqmClientException("Could not kill a job (internal error)", e);
    } finally {
        closeQuietly(em);
    }
}

From source file:org.batoo.jpa.core.impl.criteria.QueryImpl.java

private List<X> getResultListImpl() {
    this.em.getSession().setLoadTracker();

    final Connection connection = this.em.getConnection();
    try {/*from   ww w .java 2  s .co m*/
        final LockModeType lockMode = this.getLockMode();
        final boolean hasLock = (lockMode == LockModeType.PESSIMISTIC_READ)
                || (lockMode == LockModeType.PESSIMISTIC_WRITE)
                || (lockMode == LockModeType.PESSIMISTIC_FORCE_INCREMENT);
        if (hasLock) {
            this.sql = this.em.getJdbcAdaptor().applyLock(this.sql, lockMode);
        }

        final Object[] parameters = this.applyParameters(connection);

        return this.buildResultSet(connection, parameters);
    } finally {
        this.em.getSession().releaseLoadTracker();

        this.em.closeConnectionIfNecessary();
    }
}

From source file:se.nrm.dina.data.jpa.impl.DinaDaoImpl.java

@Override
public T findById(int id, Class<T> clazz, boolean isVersioned) {
    logger.info("findById - class : {} - id : {}", clazz, id);

    T tmp = null;/* www  .j a  v  a  2  s . c om*/
    try {
        if (isVersioned) {
            tmp = entityManager.find(clazz, id, LockModeType.OPTIMISTIC);
        } else {
            tmp = entityManager.find(clazz, id, LockModeType.PESSIMISTIC_READ);
        }
        entityManager.flush();
        return tmp;
    } catch (OptimisticLockException ex) {
        entityManager.refresh(tmp);
        throw new DinaDatabaseException(new ErrorBean(clazz.getSimpleName(), ex.getMessage()), 400);
    } catch (Exception ex) {
        throw new DinaDatabaseException(new ErrorBean(clazz.getSimpleName(), ex.getMessage()), 400);
    }
}