Example usage for org.hibernate LockMode UPGRADE_NOWAIT

List of usage examples for org.hibernate LockMode UPGRADE_NOWAIT

Introduction

In this page you can find the example usage for org.hibernate LockMode UPGRADE_NOWAIT.

Prototype

LockMode UPGRADE_NOWAIT

To view the source code for org.hibernate LockMode UPGRADE_NOWAIT.

Click Source Link

Document

Attempt to obtain an upgrade lock, using an Oracle-style select for update nowait.

Usage

From source file:com.ephesoft.dcma.da.service.BatchInstanceServiceImpl.java

License:Open Source License

/**
 * API to get a batch by applying Hibernate level optimistic locking and to set lock owner ino db.
 * /*from   w w w. j  a v a  2s . c  o  m*/
 * @param identifier long
 * @param lockOwner {@link ServerRegistry}
 * @throws LockAcquisitionException in case of error
 */
@Transactional
@Override
public void lockBatch(long batchId, ServerRegistry lockOwner) throws LockAcquisitionException {
    BatchInstance batchInstance = batchInstanceDao.getBatch(batchId, LockMode.UPGRADE_NOWAIT);
    if (batchInstance.getLockOwner() == null) {
        batchInstance.setLockOwner(lockOwner);
        batchInstanceDao.saveOrUpdate(batchInstance);
    } else {
        throw new LockAcquisitionException(DataAccessConstant.BATCH_ALREADY_LOCKED, null);
    }
}

From source file:com.ephesoft.dcma.da.service.BatchInstanceServiceImpl.java

License:Open Source License

/**
 * API to acquire lock on a batch instance.
 * /* w  ww  . j  a  v a  2  s  . co  m*/
 * @param batchId long
 * @throws LockAcquisitionException in case of error
 */
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public void lockBatch(long batchId) throws LockAcquisitionException {
    BatchInstance batchInstance = batchInstanceDao.getBatch(batchId, LockMode.UPGRADE_NOWAIT);
    if (batchInstance.getStatus() == BatchInstanceStatus.NEW) {
        batchInstance.setStatus(BatchInstanceStatus.LOCKED);
        batchInstanceDao.saveOrUpdate(batchInstance);
    } else {
        throw new LockAcquisitionException(DataAccessConstant.BATCH_ALREADY_LOCKED, null);
    }
}

From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java

License:Apache License

/**
 * Initializes a Criteria Query./*w  w w .  j a  va2  s . c  o  m*/
 * Mandatory Attributes:<ul>
 * <li><b>name</b>: The unqualified class name driving the criteria query.</li>
 * </ul>
 * Optional Attributes:<ul>
 * <li><b>prefix</b>: The package name of the class driving the criteria query. If null, no package is assumed.</li>
 * <li><b>maxSize</b>: The maximum number of rows to return from the database.</li>
 * <li><b>fetchSize</b>: The number of rows to fetch when rows are requested. Usually not useful for AA4H.</li>
 * <li><b>cacheEnabled</b>: Enables or disables caching for the queried objects.</li>
 * <li><b>cacheMode</b>: The cache options for the queried objects.</li>
 * <li><b>flushMode</b>: The session flush options.</li>
 * <li><b>fetchMode</b>: The collection fetch options for the query.</li>
 * <li><b>lockMode</b>: The row lock options for the queried rows.</li>
 * <li><b>timeOut</b>: The query timeout option.</li>
 * <li><b>rowCountOnly</b>: Returns a count of the query rows only.</li>
 * </ul>
 * @param attrs The attributes of the processed node.
 * @return An appended or new CriteriaSpecification
 * @throws SAXException
 */
protected CriteriaSpecification processCriteria(Attributes attrs) throws SAXException {
    if (inDetached) {
        return criteriaStack.peek();
    }
    String name = attrs.getValue("name");
    String prefix = attrs.getValue("prefix");
    if (prefix != null) {
        className = prefix + "." + name;
    } else {
        className = name;
    }
    String maxSize = attrs.getValue("maxSize");
    String fetchSize = attrs.getValue("fetchSize");
    String firstResult = attrs.getValue("firstResult");
    String cacheEnabled = attrs.getValue("cacheEnabled");
    String cacheMode = attrs.getValue("cacheMode");
    String flushMode = attrs.getValue("flushMode");
    String fetchMode = attrs.getValue("fetchMode");
    String lockMode = attrs.getValue("lockMode");
    String timeOut = attrs.getValue("timeOut");
    String rowCountOnly = attrs.getValue("rowCountOnly");
    Criteria newCriteria = null;
    try {
        if (criteriaStack.size() == 0) {
            newCriteria = session.createCriteria(className);
        } else {
            newCriteria = ((Criteria) criteriaStack.peek()).createCriteria(className);
        }
        criteriaStack.push(newCriteria);
        if ("true".equalsIgnoreCase(rowCountOnly)) {
            newCriteria.setProjection(Projections.projectionList().add(Projections.rowCount())

            );
            setRowCountOnly(true);
        }
        if (maxSize != null && isRowCountOnly() == false) {
            newCriteria.setMaxResults(Integer.parseInt(maxSize));
        }
        if (fetchSize != null && isRowCountOnly() == false) {
            newCriteria.setFetchSize(Integer.parseInt(fetchSize));
        }
        if (firstResult != null && isRowCountOnly() == false) {
            newCriteria.setFirstResult(Integer.parseInt(firstResult));
        }
        if (timeOut != null) {
            newCriteria.setTimeout(Integer.parseInt(timeOut));
        }

        if ("true".equalsIgnoreCase(cacheEnabled)) {
            newCriteria.setCacheable(true);
        } else if ("false".equalsIgnoreCase(cacheEnabled)) {
            newCriteria.setCacheable(false);
        }
        if (fetchMode != null && fetchMode.length() > 0) {
            if ("JOIN".equalsIgnoreCase(fetchMode)) {
                newCriteria.setFetchMode(name, FetchMode.JOIN);
            } else if ("SELECT".equalsIgnoreCase(fetchMode)) {
                newCriteria.setFetchMode(name, FetchMode.SELECT);
            } else {
                newCriteria.setFetchMode(name, FetchMode.DEFAULT);
            }
        } else {
            newCriteria.setFetchMode(name, FetchMode.DEFAULT);
        }
        if (cacheMode != null && cacheMode.length() > 0) {
            if ("GET".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.GET);
            } else if ("IGNORE".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.IGNORE);
            } else if ("NORMAL".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.NORMAL);
            } else if ("PUT".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.PUT);
            } else if ("REFRESH".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.REFRESH);
            } else {
                newCriteria.setCacheMode(CacheMode.NORMAL);
            }
        }
        if (lockMode != null && lockMode.length() > 0) {
            if ("NONE".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.NONE);
            } else if ("READ".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.READ);
            } else if ("UPGRADE".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.UPGRADE);
            } else if ("UPGRADE_NOWAIT".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.UPGRADE_NOWAIT);
            } else if ("WRITE".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.WRITE);
            } else {
                throw new SAXException("lockMode[" + lockMode + "] Not Recognized");
            }
        }
        if (flushMode != null && flushMode.length() > 0) {
            if ("ALWAYS".equalsIgnoreCase(flushMode)) {
                newCriteria.setFlushMode(FlushMode.ALWAYS);
            } else if ("AUTO".equalsIgnoreCase(flushMode)) {
                newCriteria.setFlushMode(FlushMode.AUTO);
            } else if ("COMMIT".equalsIgnoreCase(flushMode)) {
                newCriteria.setFlushMode(FlushMode.COMMIT);
            } else if ("NEVER".equalsIgnoreCase(flushMode)) {
                // NEVER is deprecated, so we won't throw an exception but we'll ignore it.
            } else {
                throw new SAXException("flushMode[" + flushMode + "] Not Recognized");
            }
        }
        return newCriteria;

    } catch (Exception e) {
        throw new SAXException("Unable to configure class " + className, e);
    }
}

From source file:com.sam.moca.dao.hibernate.AbstractUnknownKeyHibernateDAO.java

License:Open Source License

/**
 * This will lock the given and update at the same time.  Note that any
 * children objects may possibly be stale and if needed this object
 * should be reread after locking.//from  w  ww  .ja  v a  2  s . c om
 * Depending on the database provider there is no guarantee as to whether
 * the wait argument is paid attention to.
 * @param obj The object to lock the row on
 * @param wait Whether to wait for the lock or timeout
 * @return whether or not the lock was obtained
 */
public boolean lockRow(T obj, boolean wait) {
    try {
        LockOptions opts = new LockOptions();
        if (wait) {
            opts.setLockMode(LockMode.PESSIMISTIC_WRITE);
            opts.setTimeOut(LockOptions.WAIT_FOREVER);
        } else {
            opts.setLockMode(LockMode.UPGRADE_NOWAIT);
            opts.setTimeOut(LockOptions.NO_WAIT);
        }
        HibernateTools.getSession().refresh(obj, opts);
        return true;
    } catch (LockAcquisitionException e) {
        return false;
    }
}

From source file:org.jbpm.graph.node.JoinDbTest.java

License:Open Source License

public void testParentLockMode() {
    ProcessDefinition processDefinition = ProcessDefinition
            .parseXmlString("<process-definition name='lock mode'>" + "  <join name='read' lock='READ' />"
                    + "  <join name='nowait' lock='UPGRADE_NOWAIT' />"
                    + "  <join name='upgrade' lock='pessimistic' />" + "</process-definition>");
    deployProcessDefinition(processDefinition);

    processDefinition = graphSession.findLatestProcessDefinition("lock mode");
    Join join = (Join) processDefinition.getNode("read");
    assertEquals(LockMode.READ.toString(), join.getParentLockMode());
    join = (Join) processDefinition.getNode("nowait");
    assertEquals(LockMode.UPGRADE_NOWAIT.toString(), join.getParentLockMode());
    join = (Join) processDefinition.getNode("upgrade");
    assertEquals(LockMode.UPGRADE.toString(), join.getParentLockMode());
}

From source file:org.springframework.orm.hibernate3.HibernateTemplateTests.java

License:Apache License

@Test
public void testGetWithLockMode() throws HibernateException {
    TestBean tb = new TestBean();
    given(session.get(TestBean.class, "", LockMode.UPGRADE_NOWAIT)).willReturn(tb);
    Object result = hibernateTemplate.get(TestBean.class, "", LockMode.UPGRADE_NOWAIT);
    assertTrue("Correct result", result == tb);
    verify(session).flush();//from  w ww  .  j a v  a2  s.  c om
    verify(session).close();
}

From source file:org.springframework.orm.hibernate3.HibernateTemplateTests.java

License:Apache License

@Test
public void testGetWithEntityNameAndLockMode() throws HibernateException {
    TestBean tb = new TestBean();
    given(session.get("myEntity", "", LockMode.UPGRADE_NOWAIT)).willReturn(tb);
    Object result = hibernateTemplate.get("myEntity", "", LockMode.UPGRADE_NOWAIT);
    assertTrue("Correct result", result == tb);
    verify(session).flush();/*from w ww  .ja  v  a2s .  c  o  m*/
    verify(session).close();
}