Example usage for org.hibernate LockMode PESSIMISTIC_WRITE

List of usage examples for org.hibernate LockMode PESSIMISTIC_WRITE

Introduction

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

Prototype

LockMode PESSIMISTIC_WRITE

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

Click Source Link

Document

Transaction will obtain a database lock immediately.

Usage

From source file:org.vpac.ndg.storage.dao.TimeSliceLockDaoImpl.java

License:Open Source License

@Override
@Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public TimeSliceLock adoptOne(String adopterProcessId) throws InterruptedException {
    List<TimeSliceLock> lockList = listOrphaned();
    if (lockList.size() == 0)
        return null;

    // Lock the row of the token and try to update it.
    TimeSliceLock lockToken = lockList.get(0);
    lockToken = (TimeSliceLock) getSession().load(TimeSliceLock.class, lockToken.getId(),
            new LockOptions(LockMode.PESSIMISTIC_WRITE));

    Process proc = processDao.retrieve(lockToken.getProcessId());
    if (proc != null) {
        throw new InterruptedException(
                String.format("Lock %s was " + "apparently taken by another process.", lockToken.getId()));
    }/*from   ww  w.j  a va  2 s . c o  m*/

    lockToken.setProcessId(adopterProcessId);
    update(lockToken);
    return lockToken;
}

From source file:org.yes.cart.dao.impl.GenericDAOHibernateImpl.java

License:Apache License

/**
 * {@inheritDoc}/*w w  w .  ja va  2s .c  om*/
 */
@SuppressWarnings("unchecked")
public List<T> findByNamedQueryForUpdate(final String namedQueryName, final int timeout,
        final Object... parameters) {
    Query query = sessionFactory.getCurrentSession().getNamedQuery(namedQueryName);
    LockOptions opts = new LockOptions(LockMode.PESSIMISTIC_WRITE);
    opts.setTimeOut(timeout);
    query.setLockOptions(opts);
    if (parameters != null) {
        setQueryParameters(query, parameters);
    }
    return query.list();
}

From source file:ubic.gemma.persistence.service.expression.arrayDesign.ArrayDesignDaoImpl.java

License:Apache License

@Override
public void deleteGeneProductAssociations(ArrayDesign arrayDesign) {

    this.getSessionFactory().getCurrentSession().buildLockRequest(LockOptions.UPGRADE)
            .setLockMode(LockMode.PESSIMISTIC_WRITE).lock(arrayDesign);

    // this query is polymorphic, id gets the annotation associations?
    //language=HQL
    final String queryString = "select ba from CompositeSequence  cs "
            + "inner join cs.biologicalCharacteristic bs, BioSequence2GeneProduct ba "
            + "where ba.bioSequence = bs and cs.arrayDesign=:arrayDesign";
    List blatAssociations = this.getSessionFactory().getCurrentSession().createQuery(queryString)
            .setParameter("arrayDesign", arrayDesign).list();
    if (!blatAssociations.isEmpty()) {
        for (Object r : blatAssociations) {
            this.getSessionFactory().getCurrentSession().delete(r);
        }//  www .  j av a  2s . c  o m
        AbstractDao.log
                .info("Done deleting " + blatAssociations.size() + " blat associations for " + arrayDesign);
    }

    this.getSessionFactory().getCurrentSession().flush();

    final String annotationAssociationQueryString = "select ba from CompositeSequence cs "
            + " inner join cs.biologicalCharacteristic bs, AnnotationAssociation ba "
            + " where ba.bioSequence = bs and cs.arrayDesign=:arrayDesign";

    //noinspection unchecked
    List<AnnotationAssociation> annotAssociations = this.getSessionFactory().getCurrentSession()
            .createQuery(annotationAssociationQueryString).setParameter("arrayDesign", arrayDesign).list();

    if (!annotAssociations.isEmpty()) {

        for (AnnotationAssociation r : annotAssociations) {
            this.getSessionFactory().getCurrentSession().delete(r);
        }
        AbstractDao.log.info(
                "Done deleting " + annotAssociations.size() + " AnnotationAssociations for " + arrayDesign);

    }
}