mercredi 8 juillet 2015

Letting the presentation layer (JSF) handle the javax.persistence.OptimisticLockException

The EJB method (using CMT) that updates an entity supplied :

@Override
@SuppressWarnings("unchecked")
public boolean update(Entity entity) throws OptimisticLockException {
    // Code to merge the entity.
    return true;
}

This will throw the javax.persistence.OptimisticLockException, if concurrent update is detected which is to be handled precisely by the caller (a managed bean).

public void onRowEdit(RowEditEvent event) {
    try {
        service.update((Entity) event.getObject())
    catch(OptimisticLockException e) {
        // Add a user-friendly faces message.
    }
}

But doing so makes an additional dependency from the javax.persistence API on the presentation layer compulsory which is a design smell leading to tight-coupling.

In which exception should it be wrapped so that the tight-coupling issue can be omitted in its entirely? Or is there a standard way to handle this exception which in turn does not cause any service layer dependencies to be enforced on the presentation layer?

By the way, I found it clumsy to catch this exception in the EJB (on the service layer itself) and then return a flag value to the client (JSF).

Aucun commentaire:

Enregistrer un commentaire