Example usage for javax.persistence LockModeType WRITE

List of usage examples for javax.persistence LockModeType WRITE

Introduction

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

Prototype

LockModeType WRITE

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

Click Source Link

Document

Synonymous with OPTIMISTIC_FORCE_INCREMENT.

Usage

From source file:com.rationaldevelopers.oss.service.SimpleItemService.java

@Lock(LockModeType.WRITE)
public SimpleItem saveOrUpdate(SimpleItem simpleItem) {
    if (simpleItem != null) {
        simpleItem.setModified(Calendar.getInstance().getTime());
        if (simpleItem.getCreated() == null) {
            simpleItem.setCreated(Calendar.getInstance().getTime());
            simpleItem.setModified(simpleItem.getCreated());
        }/*from ww w.  j a  va  2  s  .  co  m*/
        return simpleItemRepository.save(simpleItem);
    }
    return simpleItem;
}

From source file:Professor.java

  public void lockAllProfessors() {
  Query query = em.createQuery("SELECT e FROM Professor e");
    /*from   ww w  .  j av a  2 s  . co  m*/
  for(Object employee: query.getResultList()){
    em.lock(employee, LockModeType.WRITE);
  }
    
    
}

From source file:com.rationaldevelopers.oss.service.SimpleItemService.java

@Lock(LockModeType.WRITE)
public void delete(SimpleItem simpleItem) {
    simpleItemRepository.delete(simpleItem);
}

From source file:fr.mby.opa.picsimpl.dao.DbPictureDao.java

@Override
public void deletePicture(final Picture picture) throws PictureNotFoundException {
    Assert.notNull(picture, "No Picture supplied !");
    Assert.notNull(picture.getId(), "Id should be set for delete !");

    new TxCallback(this.getEmf()) {

        @Override//ww w  .  j  ava  2 s.  c o m
        protected void executeInTransaction(final EntityManager em) {
            final Picture managedPicture = em.find(Picture.class, picture.getId(), LockModeType.WRITE);
            if (managedPicture == null) {
                throw new PictureNotFoundException();
            }

            em.remove(picture);
            em.lock(picture, LockModeType.NONE);
        }
    };
}

From source file:fr.mby.opa.picsimpl.dao.DbAlbumDao.java

@Override
public Album updateAlbum(final Album album) throws AlbumNotFoundException {
    Assert.notNull(album, "No Album supplied !");
    Assert.notNull(album.getId(), "Id should be set for update !");

    final TxCallbackReturn<Album> txCallback = new TxCallbackReturn<Album>(this.getEmf()) {

        @Override//  w w  w  . j  a  v  a 2s  . co m
        protected Album executeInTransaction(final EntityManager em) {
            final Album managedAlbum = em.find(Album.class, album.getId(), LockModeType.WRITE);
            if (managedAlbum == null) {
                throw new AlbumNotFoundException();
            }

            Album updatedAlbum = null;

            if (!managedAlbum.getLocked()) {
                // Update album if not locked !
                managedAlbum.setName(album.getName());
                managedAlbum.setDescription(album.getDescription());

                updatedAlbum = em.merge(managedAlbum);
            }

            return updatedAlbum;
        }
    };

    return txCallback.getReturnedValue();
}

From source file:fr.mby.opa.picsimpl.dao.DbAlbumDao.java

@Override
public void deleteAlbum(final Album album) throws AlbumNotFoundException {
    Assert.notNull(album, "No Album supplied !");
    Assert.notNull(album.getId(), "Id should be set for delete !");

    new TxCallback(this.getEmf()) {

        @Override//from w ww .ja  va  2s.  co  m
        protected void executeInTransaction(final EntityManager em) {
            final Album managedAlbum = em.find(Album.class, album.getId(), LockModeType.WRITE);
            if (managedAlbum == null) {
                throw new PictureNotFoundException();
            }

            em.remove(managedAlbum);
        }
    };
}

From source file:org.grails.datastore.mapping.jpa.JpaSession.java

public void lock(final Object o) {
    jpaTemplate.execute(new JpaCallback<Void>() {
        public Void doInJpa(EntityManager em) throws PersistenceException {
            em.lock(o, LockModeType.WRITE);
            return null;
        }//  w w w  .ja va 2 s .c o  m
    });
}

From source file:com.emc.plants.service.impl.LoginBean.java

/**
 * Update an existing user.//from  w ww . j  av  a  2s  . c  om
 *
 * @param customerID The customer ID.
 * @param firstName First name.
 * @param lastName Last name.
 * @param addr1 Address line 1.
 * @param addr2 Address line 2.
 * @param addrCity City address information.
 * @param addrState State address information.
 * @param addrZip Zip code address information.
 * @param phone User's phone number.
 * @return CustomerInfo
 */
public CustomerInfo updateUser(String customerID, String firstName, String lastName, String addr1, String addr2,
        String addrCity, String addrState, String addrZip, String phone) {
    CustomerInfo customerInfo = null;
    // TODO: lowp: no lock check is performed to see if cust data has changed since fetch!
    /*
     customerHome = (CustomerHome) Util.getEJBLocalHome("java:comp/env/ejb/Customer",
     com.ibm.websphere.samples.pbwjpa.CustomerHome.class);
             
     Customer customer = customerHome.findByPrimaryKeyUpdate(customerID);
     customer.update(firstName, lastName, addr1, addr2, addrCity, addrState, addrZip, phone);
     customerInfo = new CustomerInfo(customer);
     */
    EntityManager em = entityManagerFactory.createEntityManager();
    em.getTransaction().begin();
    Customer c = em.find(Customer.class, customerID);
    em.lock(c, LockModeType.WRITE);
    em.refresh(c);
    // TODO: lowp: test and set for update performance?
    c.setFirstName(firstName);
    c.setLastName(lastName);
    c.setAddr1(addr1);
    c.setAddr2(addr2);
    c.setAddrCity(addrCity);
    c.setAddrState(addrState);
    c.setAddrZip(addrZip);
    c.setPhone(phone);

    customerInfo = new CustomerInfo(c);
    em.getTransaction().commit();
    return customerInfo;
}

From source file:com.emc.plants.service.impl.ShoppingCartBean.java

/**
 * Create a BackOrder of this inventory item.
 * @param quantity The number of the inventory item to be backordered
 *//*from  w ww . ja  va2s . co m*/
private void backOrder(Inventory inv, int amountToOrder) {
    //EntityManager em = entityManagerFactory.createEntityManager();
    //   BackOrder b=em.find(BackOrder.class, inv.getBackOrder().getBackOrderID());
    BackOrder b = inv.getBackOrder();
    if (b == null) {
        //create a new backorder if none exists
        BackOrder newBO = new BackOrder(inv, amountToOrder);
        System.out.println("coming to back order 1");
        em.persist(newBO);
        em.flush();
        inv.setBackOrder(newBO);
    } else {
        //update the backorder with the new quantity         
        int quantity = b.getQuantity();
        quantity += amountToOrder;
        System.out.println("coming to back order 11");
        em.lock(b, LockModeType.WRITE);
        em.refresh(b);
        b.setQuantity(quantity);
        em.flush();
        inv.setBackOrder(b);
    }
}

From source file:com.emc.plants.service.impl.CatalogBean.java

/**
 * Get a remote Inventory object to Update.
 *
 * @param inventoryID The id of the inventory item wanted.
 * @return Reference to the remote Inventory object.
 *//*from  w  w  w.java 2 s  .c om*/
private Inventory getInvUpdate(String inventoryID) {
    Inventory inv = null;
    /*
     InventoryHome invHome = (InventoryHome) Util.getEJBLocalHome("java:comp/env/ejb/Inventory",
     com.ibm.websphere.samples.pbwjpa.InventoryHome.class);
     inv = invHome.findByPrimaryKeyUpdate(inventoryID);
     */
    // TODO: lowp: eventually replace with find for update hint
    inv = em.find(Inventory.class, inventoryID);
    em.lock(inv, LockModeType.WRITE);
    em.refresh(inv);
    return inv;
}