6 Replies Latest reply on Mar 22, 2010 8:51 PM by cash1981

    FacesMessage after exception

    mvlach

      I'm facing problem with facesMessages. Let's imagine this code:


      @End
      public String save() {
       ...
      
       facesMessages.addFromResourceBundle(...);
       em.persist(instance);
      }
      



      The facesMessage is added and the transaction failed. You show another page and you get the previous message, for example: profile has been updated.


      But toe profile.save action ends with exception....


      Know somebody how to solve this problem ?


      Thanks MIla

        • 1. Re: FacesMessage after exception
          ssamayoagt

          May be this?


          @End
          public String save() {
          ...


          try {
             em.persist(instance);
             // Persisted message
             facesMessages.addFromResourceBundle(...);
          } catch(Excpetion ex) {
             // Something is wrong
             facesMessages.addFromResourceBundle(...);
             ...
          }
          }


          Regards.

          • 2. Re: FacesMessage after exception
            cash1981

            Probably because when exception occurs your facesMessage isnt executed. What Sergio suggest would be the correct way. (Or you can add error message from pages.xml handling the exception)

            • 3. Re: FacesMessage after exception
              mvlach

              I think you both are not right, because:


              not thrown an exception. The exception can occurs in seam code, not in this function. The problem is that the facesMessages is added to the Seam. In persist is not called database queries and the possible error cannot be visible:


              Then occurs error for example with unique keys, the hibernate will throw an exception very later, but the message All is successfully updated appears in second page invocation.


              Doesn't make sense.


              Thanks Mila

              • 4. Re: FacesMessage after exception
                cash1981

                No we are not wrong. Its only you who have not enough knowledge of how Seam works.


                If you look in your logs you will probably see A message has been queued but not shown. (Or something similar).


                This means you should do a proper redirect. That way it will show the message. Put a <redirect/> in pages.xml for that method, and the error message should appear in the same page the error occurs. (If you do not have error handling in pages.xml).


                Also, if exception is thrown in your line em.persist, then facesMessage will in your exception will get invoked. Easy as that. That is how try catch works in programming languages.

                • 5. Re: FacesMessage after exception
                  chits98

                  What if there is no exception when calling persist,  but an exception occurs during commit?

                  • 6. Re: FacesMessage after exception
                    cash1981

                    Pauline Daniel wrote on Mar 21, 2010 20:16:


                    What if there is no exception when calling persist,  but an exception occurs during commit?


                    The persist call will do a commit, thus it is that call that will catch the exception. So its the same.