1

I've looked around for help with this issue but actually I don't have any idea where the problem is coming from so I don't know what to look for.

To be simple: - I have a form that persist an object in DB in Ajax generated by JSF using JPA. - When there is a PersistenceException I want to handle it and send a message to the user.

In my DAO I catch PersistenceException and throw a new custom DAOException to my Backing Bean in order to add a FacesMessage to the view.

Facelet :

    <h:outputLabel for="codePF" styleClass="control-label">Code PF <span class="requis">*</span>
                    </h:outputLabel>
                    <h:inputText id="codePF" value="#{gestionDemandes.demande.motCle}" required="true" size="20" maxlength="20"  styleClass="form-control">
                        <f:ajax event="blur" render="codePFMessage" />
                    </h:inputText>
                    <h:message id="codePFMessage" for="codePF" errorClass="erreur" />
                    <br />

                    <h:messages globalOnly="true" infoClass="info" />

                    <h:commandButton id="boutonDemande" value="Inscription" action="#{gestionDemandes.creerDemande()}" styleClass="btn btn-primary">
                        <f:ajax execute="@form" render="@form" />
                        <h:message id="boutonDemandeMessage" for="boutonDemande" errorClass="bg-danger"  infoClass="bg-success"  />
                    </h:commandButton>
                    <br />
                </h:form>

Backing Bean:

   @ManagedBean
   @RequestScoped
public class GestionDemandes implements Serializable {
private static final String ERREUR_AJOUT = "Erreur de création de la demande";
private static final String SUCCES = "Demande ajoutée !";

// Injection de notre EJB (Session Bean Stateless)
@EJB
private DAODemande daoDemande;
// Demande utilisée par le formulaire de création de demandes
public void creerDemande() {

FacesMessage message;
try {
    daoDemande.creer(demande);
} catch (DAOException dao) {
    message = new FacesMessage(FacesMessage.SEVERITY_ERROR, ERREUR_AJOUT,
        dao.getMessage());
    FacesContext.getCurrentInstance().addMessage(null, message);
    return;
}
message = new FacesMessage(SUCCES);
FacesContext.getCurrentInstance().addMessage(null, message);
}

DAO:

@Stateless
public class DAODemande {
// Injection du manager
@PersistenceContext(unitName = "gamabddactuelle_PU")
private EntityManager em;

// Enregistrement d'un nouvel utilisateur
public void creer(Demande demande) throws DAOException {

demande.setIdDemande(demande.getIdClientDemande());
try {
    em.persist(demande);
    em.flush();
} catch (PersistenceException e) {
    throw new DAOException(e.getCause());
}
}

First, I had to add the em.flush() in DAO or I wouldn't even catch the PersistenceException.

Then when I throw my DAOException, I don't catch it in my Backing Bean. It seems the DAOException is thrown in an other thread than the Ajax Call where I persist my Object.

I read this : Better Exception Handling in JPA

Honestly I found it a bit complicated just to handle a simple Exception.

Can someone explain what is going on here ? Is it a thread problem ?

My Conf: TOMEE 7 with/Openjpa 2.4/myfaces 2.2.8 JRE 1.8

Community
  • 1
  • 1
  • Alright, I added some context. I do have a , I don't know why a h:message in a commandButton is illogical I'm a newbie in JSF, yes I'm using EJB 3, and I don't have @Asynchronous annotation anywhere. Thanks for your time. – Pierre-Yves Rouillé May 20 '15 at 18:43
  • I changed the server from TomEE to Wildfly, and still have the same problem. It's not related tojpa libraries at least... Also, I bypassed my custom Exception and threw directly the persistence exception to the backing bean. Still the same issue. – Pierre-Yves Rouillé May 22 '15 at 22:34
  • Hi, I have a similar problem. My managedBean just doesn't catch the exception thrown by DAO layer. How did you solve it? – Akshat Apr 29 '16 at 06:59
  • Haha, did not. That was the end of my jsf adventure! – Pierre-Yves Rouillé May 01 '16 at 10:49
  • Actually I did solve it. I will answer it, see if it is acceptable to you. – Akshat May 02 '16 at 08:55

1 Answers1

0

I had a similar issue and after hours of breaking my head, I found what was actually happening.

When my DAO had an exception and when I threw a custom app exception from DAO, I realized that when still in my transaction scope, the custom exception was overridden by hibernate's exception and hence my view layer could not catch my application's custom exception.

All I had to do is handle in my transaction the exception and rollBack the transaction. (It was an insert operation)

Things to do:

  1. Check if hibernate is overridden your exception

  2. In your transaction handle the hibernate exception (I did a rollback handle)

  3. Make sure the custom app exception is thrown all the way up to the view layer.

Akshat
  • 278
  • 5
  • 19