Example usage for javax.persistence LockModeType PESSIMISTIC_WRITE

List of usage examples for javax.persistence LockModeType PESSIMISTIC_WRITE

Introduction

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

Prototype

LockModeType PESSIMISTIC_WRITE

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

Click Source Link

Document

Pessimistic write lock.

Usage

From source file:org.kuali.rice.kew.routeheader.dao.impl.DocumentRouteHeaderDAOJpa.java

@Override
public void lockRouteHeader(final String documentId) {
    // passing a hint here on the lock timeout, this will really only work on Oracle since it supports "wait"
    // on a SELECT ... FOR UPDATE but other databases don't
    ////from   w  w w  . j ava 2 s.co  m
    // one random additional piece of trivia to note, if the timeout comes back non-zero, then EclipseLink just
    // ingores it when sending it to MySQL and issues a plain SELECT ... FOR UPDATE. However if it cames back as 0,
    // it will try to issue a SELECT ... FOR UPDATE NOWAIT even thouh MySQL doesn't support it. This, of course,
    // triggers an exception from the MySQL database
    //
    // the moral of the story? don't ever set the timeout to zero, at least not until EclipseLink fixes that bug
    Map<String, Object> options = new HashMap<String, Object>();
    options.put(LOCK_TIMEOUT_HINT, getTimeoutMilliseconds());
    getEntityManager().find(DocumentRouteHeaderValue.class, documentId, LockModeType.PESSIMISTIC_WRITE,
            options);
}

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

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

    // Entity has no version can not have Optimistic lock
    if (clazz.getSimpleName().equals(Recordsetitem.class.getSimpleName())
            || clazz.getSimpleName().equals(Sppermission.class.getSimpleName())
            || clazz.getSimpleName().equals(Workbenchrow.class.getSimpleName())
            || clazz.getSimpleName().equals(Workbenchdataitem.class.getSimpleName())
            || clazz.getSimpleName().equals(Workbenchrowimage.class.getSimpleName())
            || clazz.getSimpleName().equals(Geoname.class.getSimpleName())) {

        return entityManager.find(clazz, id, LockModeType.PESSIMISTIC_WRITE);
    }// w ww.  j  ava  2s . c  om

    T tmp = null;
    try {
        tmp = entityManager.find(clazz, id, LockModeType.OPTIMISTIC);
        entityManager.flush();
    } catch (OptimisticLockException ex) {
        entityManager.refresh(tmp);
        logger.warn(ex.getMessage());
    } catch (Exception ex) {
        logger.warn(ex.getMessage());
    }
    return tmp;
}