JSF/JPA/EJB Best practice for locking an entity from the "Edit" page

508 views Asked by At

We have a basic JSF/EJB/JPA web application in which the EJB business beans work with JPA / entity manager and manage all transactions.

That is:

Page.xhtml => PageBean.java => BusinessBean.java => Entity.java

Where Page.xhtml displays values for an entity. PageBean retrieves the entity from the business layer via a method call (ex. BusinessBean.getEntityById(x)). The business bean retrieves the entity like this:

public Entity getEntityById(String id) {
  return EntityManager.find(Entity.class, id);
}

The entity is then edited in the presentation layer and when the form is submitted a business method is called from the backing page bean on the business bean:

public Entity saveExistingEntity(Entity e) {
  Entity exists = entityManager.find(Entity.class, e.getId())
  if(exists != null) {
    entityManager.merge(e);
  }
  return e;
}

I'm not sure if this is the best way to do this, and welcome comment on this method.

However, the core question relates to managing locking. The requirement is that when a user enters the 'edit' page for the entity, the entity becomes locked and other users are not allowed to edit it via any other method (for example, entering the edit page themselves for that entity).

How can we structure our page backing bean and business logic to allow this to happen and prevent semi/permanent locks if the user disappears? Since the transaction begins and ends in the business layer, I'm not sure how we can lock it and keep that lock while the user interacts in the presentation layer.

0

There are 0 answers