Example usage for javax.persistence EntityManager setProperty

List of usage examples for javax.persistence EntityManager setProperty

Introduction

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

Prototype

public void setProperty(String propertyName, Object value);

Source Link

Document

Set an entity manager property or hint.

Usage

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 w w  .ja  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();
    }
}