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:net.groupbuy.dao.impl.SnDaoImpl.java

private long getLastValue(Type type) {
    String jpql = "select sn from Sn sn where sn.type = :type";
    Sn sn = entityManager.createQuery(jpql, Sn.class).setFlushMode(FlushModeType.COMMIT)
            .setLockMode(LockModeType.PESSIMISTIC_WRITE).setParameter("type", type).getSingleResult();
    long lastValue = sn.getLastValue();
    sn.setLastValue(lastValue + 1);/*from  w w  w.  j a va2  s .  co m*/
    entityManager.merge(sn);
    return lastValue;
}

From source file:net.groupbuy.service.impl.MemberServiceImpl.java

public void update(Member member, Integer modifyPoint, BigDecimal modifyBalance, String depositMemo,
        Admin operator) {/*from w w w .  j  a  va  2  s  .co m*/
    Assert.notNull(member);

    memberDao.lock(member, LockModeType.PESSIMISTIC_WRITE);

    if (modifyPoint != null && modifyPoint != 0 && member.getPoint() + modifyPoint >= 0) {
        member.setPoint(member.getPoint() + modifyPoint);
    }

    if (modifyBalance != null && modifyBalance.compareTo(new BigDecimal(0)) != 0
            && member.getBalance().add(modifyBalance).compareTo(new BigDecimal(0)) >= 0) {
        member.setBalance(member.getBalance().add(modifyBalance));
        Deposit deposit = new Deposit();
        if (modifyBalance.compareTo(new BigDecimal(0)) > 0) {
            deposit.setType(operator != null ? Deposit.Type.adminRecharge : Deposit.Type.memberRecharge);
            deposit.setCredit(modifyBalance);
            deposit.setDebit(new BigDecimal(0));
        } else {
            deposit.setType(operator != null ? Deposit.Type.adminChargeback : Deposit.Type.memberPayment);
            deposit.setCredit(new BigDecimal(0));
            deposit.setDebit(modifyBalance);
        }
        deposit.setBalance(member.getBalance());
        deposit.setOperator(operator != null ? operator.getUsername() : null);
        deposit.setMemo(depositMemo);
        deposit.setMember(member);
        depositDao.persist(deposit);
    }
    memberDao.merge(member);
}

From source file:com.pinterest.rocksplicator.controller.mysql.MySQLTaskQueue.java

@Override
public boolean lockCluster(final Cluster cluster) {
    beginTransaction();//w ww.j a va  2s.  c  om
    TagEntity tagEntity = getEntityManager().find(TagEntity.class, new TagId(cluster),
            LockModeType.PESSIMISTIC_WRITE);
    try {
        if (tagEntity == null) {
            LOG.error("Cluster {} hasn't been created", cluster);
            throw new MySQLTaskQueueException();
        }
        if (tagEntity.getLocks() == 1) {
            LOG.error("Cluster {} is already locked, cannot double lock", cluster);
            throw new MySQLTaskQueueException();
        }
    } catch (MySQLTaskQueueException e) {
        getEntityManager().getTransaction().rollback();
        return false;
    }
    tagEntity.setLocks(1);
    getEntityManager().persist(tagEntity);
    getEntityManager().getTransaction().commit();
    return true;
}

From source file:net.shopxx.service.impl.MerchantServiceImpl.java

public void update(Merchant merchant, Integer modifyPoint, BigDecimal modifyBalance, String depositMemo,
        Admin operator) {/*from w  w w .jav  a 2  s .  co m*/
    Assert.notNull(merchant);

    merchantDao.lock(merchant, LockModeType.PESSIMISTIC_WRITE);

    if (modifyPoint != null && modifyPoint != 0 && merchant.getPoint() + modifyPoint >= 0) {
        merchant.setPoint(merchant.getPoint() + modifyPoint);
    }

    if (modifyBalance != null && modifyBalance.compareTo(new BigDecimal(0)) != 0
            && merchant.getBalance().add(modifyBalance).compareTo(new BigDecimal(0)) >= 0) {
        merchant.setBalance(merchant.getBalance().add(modifyBalance));
        Deposit deposit = new Deposit();
        if (modifyBalance.compareTo(new BigDecimal(0)) > 0) {
            //            deposit.setType(operator != null ? Deposit.Type.adminRecharge : Deposit.Type.merchantRecharge);
            deposit.setCredit(modifyBalance);
            deposit.setDebit(new BigDecimal(0));
        } else {
            //            deposit.setType(operator != null ? Deposit.Type.adminChargeback : Deposit.Type.merchantPayment);
            deposit.setCredit(new BigDecimal(0));
            deposit.setDebit(modifyBalance);
        }
        deposit.setBalance(merchant.getBalance());
        deposit.setOperator(operator != null ? operator.getUsername() : null);
        deposit.setMemo(depositMemo);
        deposit.setMerchant(merchant);
        depositDao.persist(deposit);
    }
    merchantDao.merge(merchant);
}

From source file:org.jasig.cas.ticket.registry.JpaTicketRegistry.java

private Ticket getRawTicket(final String ticketId) {
    try {/*w w w. j  ava2  s  . co  m*/
        if (ticketId.startsWith(this.ticketGrantingTicketPrefix)) {
            return entityManager.find(TicketGrantingTicketImpl.class, ticketId, LockModeType.PESSIMISTIC_WRITE);
        }

        return entityManager.find(ServiceTicketImpl.class, ticketId);
    } catch (final Exception e) {
        log.error("Error getting ticket " + ticketId + " from registry.", e);
    }
    return null;
}

From source file:com.dp2345.service.impl.ShopServiceImpl.java

public void update(Shop shop, Integer modifyPoint, BigDecimal modifyBalance, String depositMemo,
        Admin operator) {//  w  ww.j  a  v  a 2 s  . c  o  m
    Assert.notNull(shop);

    shopDao.lock(shop, LockModeType.PESSIMISTIC_WRITE);

    if (modifyPoint != null && modifyPoint != 0 && shop.getPoint() + modifyPoint >= 0) {
        shop.setPoint(shop.getPoint() + modifyPoint);
    }
    /*
          if (modifyBalance != null && modifyBalance.compareTo(new BigDecimal(0)) != 0 && shop.getBalance().add(modifyBalance).compareTo(new BigDecimal(0)) >= 0) {
             shop.setBalance(shop.getBalance().add(modifyBalance));
             Deposit deposit = new Deposit();
             if (modifyBalance.compareTo(new BigDecimal(0)) > 0) {
    deposit.setType(operator != null ? Deposit.Type.adminRecharge : Deposit.Type.shopRecharge);
    deposit.setCredit(modifyBalance);
    deposit.setDebit(new BigDecimal(0));
             } else {
    deposit.setType(operator != null ? Deposit.Type.adminChargeback : Deposit.Type.shopPayment);
    deposit.setCredit(new BigDecimal(0));
    deposit.setDebit(modifyBalance);
             }
             deposit.setBalance(shop.getBalance());
             deposit.setOperator(operator != null ? operator.getUsername() : null);
             deposit.setMemo(depositMemo);
             deposit.setShop(shop);
             depositDao.persist(deposit);
          }
          */
    shopDao.merge(shop);
}

From source file:net.kamhon.ieagle.dao.JpaDao.java

@Override
public T getForUpdate(final Class<T> clazz, final Serializable serializablekey) {
    return getJpaTemplate().execute(new JpaCallback<T>() {
        @Override//from  w ww .j ava 2 s . c  o  m
        public T doInJpa(EntityManager em) throws PersistenceException {
            return em.find(clazz, serializablekey, LockModeType.PESSIMISTIC_WRITE);
        }
    });
}

From source file:org.jasig.cas.ticket.registry.support.JpaLockingStrategy.java

/** {@inheritDoc} */
@Transactional(readOnly = false)/* w ww.  j ava 2  s  . c  o  m*/
public boolean acquire() {
    Lock lock;
    try {
        lock = entityManager.find(Lock.class, applicationId, LockModeType.PESSIMISTIC_WRITE);
    } catch (PersistenceException e) {
        logger.debug("{} failed querying for {} lock.", new Object[] { uniqueId, applicationId, e });
        return false;
    }

    boolean result = false;
    if (lock != null) {
        final Date expDate = lock.getExpirationDate();
        if (lock.getUniqueId() == null) {
            // No one currently possesses lock
            logger.debug("{} trying to acquire {} lock.", uniqueId, applicationId);
            result = acquire(entityManager, lock);
        } else if (expDate != null && new Date().after(expDate)) {
            // Acquire expired lock regardless of who formerly owned it
            logger.debug("{} trying to acquire expired {} lock.", uniqueId, applicationId);
            result = acquire(entityManager, lock);
        }
    } else {
        // First acquisition attempt for this applicationId
        logger.debug("Creating {} lock initially held by {}.", applicationId, uniqueId);
        result = acquire(entityManager, new Lock());
    }
    return result;
}

From source file:com.sammyun.service.impl.MemberServiceImpl.java

/**
 * //w w  w . j a v  a  2  s. co m
 * 
 * @param member 
 * @param modifyPoint 
 * @param modifyBalance ?
 * @param depositMemo ?
 * @param operator ?
 */
public void update(Member member, Integer modifyPoint, Admin operator) {
    Assert.notNull(member);

    memberDao.lock(member, LockModeType.PESSIMISTIC_WRITE);

    if (modifyPoint != null && modifyPoint != 0 && member.getPoint() + modifyPoint >= 0) {
        member.setPoint(member.getPoint() + modifyPoint);
    }

    memberDao.merge(member);
}

From source file:com.pinterest.rocksplicator.controller.mysql.MySQLTaskQueue.java

@Override
public boolean unlockCluster(final Cluster cluster) {
    beginTransaction();//from w  w w  .j a  va 2 s . c o m
    TagEntity tagEntity = getEntityManager().find(TagEntity.class, new TagId(cluster),
            LockModeType.PESSIMISTIC_WRITE);
    if (cluster == null) {
        LOG.error("Cluster {} hasn't been created", cluster);
        getEntityManager().getTransaction().rollback();
        return false;
    }
    tagEntity.setLocks(0);
    getEntityManager().persist(tagEntity);
    getEntityManager().getTransaction().commit();
    return true;
}