0 Replies Latest reply on Jul 1, 2008 12:11 PM by gonzalad

    PC and conversation stable when rollbacking in action code ?

    gonzalad

      Hello,


      Are the persistent context and conversation context (flushMode=MANUAL, LR conversation) considered to be stable in case I rollback the current transaction
      (Transaction.instance().setRollbackOnly()) in my action code ?


      If the state is stable, I can show the input page to the user in order for him to correct the error.


      If the state us inconsistent, I'll consider the error as fatal, end the current conversation, show him the main error page.


      I'm using the sample code in shown in [1].


      Thanks very much for your input !



      P.S. : I know that this sample is so-so (asking for rollback here is unnecessary if I don't flush (flushMode=MANUAL)).


      [1]


      @End
      public String update(Client aClient) throws MatriculeExistantException {
          
                      //validating unique constraints
          if (! aClient.getId().equals (findIdForMatricule(aClient.getPersonne().getMatricule()))) {
                                      Transaction.instance().setRollbackOnly();               
                                      super.addFacesMessage(IClefErreur.CODE_CLIENT_EXISTANT);                                
              return null;
          }
          
                      return "success";
      }
      
      //utility method (constraint validation)
      private Long findIdForMatricule(String aMatricule) {
          String lRequest = "SELECT c.id FROM Client c " +
          "WHERE c.personne.matricule=:matricule ";
          Query lQuery = getEntityManager().createQuery(lRequest);
          // On précise la clef primaire
          lQuery.setParameter("matricule", aMatricule);
      
          // - getResultList vs getSingleResult -
          // cette méthode retourne null si aucun client n'est enregistré sous le matricule
          // aMatricule.
          // On fait donc appel à getResultList qui ne génère pas d'erreur si
          // la requête ne retourne aucun enregistrement (contrairement à getSingleResult()). 
          List lResultList = lQuery.getResultList();
          if (lResultList.size() ==0) {
              return null;
          } else {
              return (Long) lResultList.get (0);
          }
      }