Example usage for javax.persistence EntityManager find

List of usage examples for javax.persistence EntityManager find

Introduction

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

Prototype

public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode);

Source Link

Document

Find by primary key and 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 ww.  j  a va  2 s  .c  o 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:com.enioka.jqm.api.HibernateClient.java

@Override
public void cancelJob(int idJob) {
    EntityManager em = null;
    JobInstance ji = null;//from www. j  a v a  2s .c om
    try {
        em = getEm();
        em.getTransaction().begin();
        ji = em.find(JobInstance.class, idJob, LockModeType.PESSIMISTIC_WRITE);
        if (ji.getState().equals(State.SUBMITTED)) {
            ji.setState(State.CANCELLED);
        } else {
            throw new NoResultException();
        }
        em.getTransaction().commit();
    } catch (NoResultException e) {
        closeQuietly(em);
        throw new JqmClientException(
                "the job is already running, has already finished or never existed to begin with");
    }

    try {
        em.getTransaction().begin();
        History h = new History();
        h.setId(ji.getId());
        h.setJd(ji.getJd());
        h.setApplicationName(ji.getJd().getApplicationName());
        h.setSessionId(ji.getSessionID());
        h.setQueue(ji.getQueue());
        h.setQueueName(ji.getQueue().getName());
        h.setEnqueueDate(ji.getCreationDate());
        h.setUserName(ji.getUserName());
        h.setEmail(ji.getEmail());
        h.setParentJobId(ji.getParentId());
        h.setApplication(ji.getApplication());
        h.setModule(ji.getModule());
        h.setKeyword1(ji.getKeyword1());
        h.setKeyword2(ji.getKeyword2());
        h.setKeyword3(ji.getKeyword3());
        h.setProgress(ji.getProgress());
        h.setStatus(State.CANCELLED);
        h.setNode(ji.getNode());
        if (ji.getNode() != null) {
            h.setNodeName(ji.getNode().getName());
        }
        em.persist(h);

        em.createQuery("DELETE FROM JobInstance WHERE id = :i").setParameter("i", ji.getId()).executeUpdate();
        em.getTransaction().commit();
    } catch (Exception e) {
        throw new JqmClientException("could not cancel job instance", e);
    } finally {
        closeQuietly(em);
    }
}

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

@Override
public void setJobQueuePosition(int idJob, int position) {
    EntityManager em = null;
    JobInstance ji = null;//w  w  w.ja  v a2  s  .  c om
    try {
        em = getEm();
        em.getTransaction().begin();
        ji = em.find(JobInstance.class, idJob, LockModeType.PESSIMISTIC_WRITE);
    } catch (Exception e) {
        closeQuietly(em);
        throw new JqmClientException(
                "Could not lock a job by the given ID. It may already have been executed or a timeout may have occurred.",
                e);
    }

    if (!ji.getState().equals(State.SUBMITTED)) {
        closeQuietly(em);
        throw new JqmInvalidRequestException(
                "Job is already set for execution. Too late to change its position in the queue");
    }

    try {
        int current = ji.getCurrentPosition(em);
        int betweenUp = 0;
        int betweenDown = 0;

        if (current == position) {
            // Nothing to do
            em.getTransaction().rollback();
            return;
        } else if (current < position) {
            betweenDown = position;
            betweenUp = position + 1;
        } else {
            betweenDown = position - 1;
            betweenUp = position;
        }

        // No locking - we'll deal with exceptions
        List<JobInstance> currentJobs = em
                .createQuery("SELECT ji from JobInstance ji ORDER BY ji.internalPosition", JobInstance.class)
                .setMaxResults(betweenUp).getResultList();

        if (currentJobs.isEmpty()) {
            ji.setInternalPosition(0);
        } else if (currentJobs.size() < betweenUp) {
            ji.setInternalPosition(currentJobs.get(currentJobs.size() - 1).getInternalPosition() + 0.00001);
        } else {
            // Normal case: put the JI between the two others.
            ji.setInternalPosition((currentJobs.get(betweenUp - 1).getInternalPosition()
                    + currentJobs.get(betweenDown - 1).getInternalPosition()) / 2);
        }
        em.getTransaction().commit();
    } catch (Exception e) {
        throw new JqmClientException("could not change the queue position of a job (internal error)", e);
    } finally {
        closeQuietly(em);
    }
}

From source file:gov.osti.services.Metadata.java

/**
 * Obtain a reserved DOI value if possible.
 *
 * @return a DoiReservation if successful, or null if not
 *///from w  ww. j  a v  a  2 s.c  o  m
private static DoiReservation getReservedDoi() {
    EntityManager em = DoeServletContextListener.createEntityManager();
    // set a LOCK TIMEOUT to prevent collision
    em.setProperty("javax.persistence.lock.timeout", 5000);

    try {
        em.getTransaction().begin();

        DoiReservation reservation = em.find(DoiReservation.class, DoiReservation.TYPE,
                LockModeType.PESSIMISTIC_WRITE);

        if (null == reservation)
            reservation = new DoiReservation();

        reservation.reserve();

        em.merge(reservation);

        em.getTransaction().commit();

        // send it back
        return reservation;
    } catch (PessimisticLockException | LockTimeoutException e) {
        log.warn("DOI Reservation, unable to obtain lock.", e);
        return null;
    } finally {
        em.close();
    }
}