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.jasig.cas.ticket.registry.JpaTicketRegistry.java

private void deleteTicketAndChildren(final Ticket ticket) {
    final List<TicketGrantingTicketImpl> ticketGrantingTicketImpls = entityManager
            .createQuery("select t from TicketGrantingTicketImpl t where t.ticketGrantingTicket.id = :id",
                    TicketGrantingTicketImpl.class)
            .setLockMode(LockModeType.PESSIMISTIC_WRITE).setParameter("id", ticket.getId()).getResultList();
    final List<ServiceTicketImpl> serviceTicketImpls = entityManager
            .createQuery("select s from ServiceTicketImpl s where s.ticketGrantingTicket.id = :id",
                    ServiceTicketImpl.class)
            .setParameter("id", ticket.getId()).getResultList();

    for (final ServiceTicketImpl s : serviceTicketImpls) {
        removeTicket(s);/*w w w .j  av  a 2s  .  c o m*/
    }

    for (final TicketGrantingTicketImpl t : ticketGrantingTicketImpls) {
        deleteTicketAndChildren(t);
    }

    removeTicket(ticket);
}

From source file:business.services.UserService.java

@Transactional
public User save(User user) throws EmailAddressNotAvailable {
    assert (user.getRoles().size() == 1);
    synchronized (lock) {
        em.persist(user);/*ww w  .  j  av  a 2s.  c om*/
        em.flush();
        em.refresh(user, LockModeType.PESSIMISTIC_WRITE);
        em.flush();
        long count = userRepository.countByUsernameAndDeletedFalse(user.getUsername());
        if (count > 1) {
            throw new EmailAddressNotUnique();
        }
    }
    return user;
}

From source file:com.vladmihalcea.HibernateCascadeLockComponentTest.java

@Test
public void test() {

    final Long parentId = cleanAndSaveParent();

    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override//from  w  w w . j a  va  2 s.c o m
        public Void doInTransaction(TransactionStatus transactionStatus) {
            Post post = entityManager.find(Post.class, parentId);
            entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE, Collections
                    .singletonMap("javax.persistence.lock.scope", (Object) PessimisticLockScope.EXTENDED));
            return null;
        }
    });
}

From source file:org.wallride.repository.CategoryRepositoryImpl.java

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<Category> root = query.from(Category.class);
    query.select(root.get(Category_.id));
    query.where(cb.equal(root.get(Category_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

From source file:org.wallride.repository.PostRepositoryImpl.java

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<Post> root = query.from(Post.class);
    query.select(root.get(Post_.id));//from ww w .j a  va 2s  . co m
    query.where(cb.equal(root.get(Post_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

From source file:org.wallride.repository.CustomFieldRepositoryImpl.java

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<CustomField> root = query.from(CustomField.class);
    query.select(root.get(CustomField_.id));
    query.where(cb.equal(root.get(CustomField_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

From source file:com.htmlhifive.sync.resource.common.ResourceItemCommonDataRepository.java

/**
 * ????ID?????????????.<br>//from  w  ww .j  a  va  2 s  .c  om
 * ???.
 *
 * @param resourceName ??
 * @param targetItemId ?ID
 * @param since ?(??)
 * @return ??
 */
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT d FROM ResourceItemCommonData d WHERE d.id.resourceName = :resourceName AND d.targetItemId = :targetItemId AND d.lastModified > :since")
ResourceItemCommonData findModifiedForUpdate(@Param("resourceName") String resourceName,
        @Param("targetItemId") String targetItemId, @Param("since") long since);

From source file:business.services.RequestNumberService.java

/**
 * Generate a new request number with format <code>YYYY-N</code>, where <var>YYYY</var> is the current
 * year and <var>N</var> is a sequence number, starting at 1 for every year.
 * @return the new request number.//from w w  w . j  a  v  a 2  s. co m
 */
public String getNewRequestNumber() {
    int year = Calendar.getInstance().get(Calendar.YEAR);
    RequestNumber number;
    synchronized (lock) {
        number = requestNumberRepository.findByYear(year);
        if (number == null) {
            log.info("Create new request number counter for the year " + year + ".");
            number = new RequestNumber(year);
            em.persist(number);
            em.flush();
        }
    }
    em.refresh(number, LockModeType.PESSIMISTIC_WRITE);
    number.setSerialNumber(number.getSerialNumber() + 1);
    em.persist(number);
    em.flush();
    log.info("Handing out new request number: " + number.toString());
    return number.toString();
}

From source file:eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryMaintenanceImpl.java

protected void allocatePersistentIdentifiers(EntityManager em, final Date nowDateAlloc) {
    em.getTransaction().begin();//from  w  w w. j a v a 2 s .  c o m
    TypedQuery<VirtualCollection> q = em.createNamedQuery("VirtualCollection.findAllByStates",
            VirtualCollection.class);
    List<VirtualCollection.State> states = new LinkedList<>();
    states.add(VirtualCollection.State.PUBLIC_PENDING);
    states.add(VirtualCollection.State.PUBLIC_FROZEN_PENDING);
    q.setParameter("states", states);
    q.setParameter("date", nowDateAlloc);
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    for (VirtualCollection vc : q.getResultList()) {
        allocatePersistentIdentifier(em, vc);
    }
    em.getTransaction().commit();
}

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

@SuppressWarnings("unchecked")
public T getForUpdate(final PK serializablekey) {
    return em.find((Class<T>) t.getClass(), serializablekey, LockModeType.PESSIMISTIC_WRITE);
}