2 Replies Latest reply on Jun 24, 2009 6:31 PM by acerberus

    NoResultException causes transaction to rollback

    acerberus

      Hi,


      I am trying to validate an entity on persist time. For this I extended the Abstract Home class and overrode the persist method. For validating the entity I have to run a query against the database, which if successful should result in a NoResultException. If that is the case I want to continue with the persist. Here is the code I am using. The UserTreeUtil component runs the query and returns null if it catches the NoResultException



      @Name("rs.RoleHome")
      @AutoCreate
      public class RoleHome extends EntityHome<Role> {
      
           @In("rs.UserTreeUtil") private UserTreeUtil userTreeUtil;
           
           /**
            * Make sure that the unique constraint on name is validated
            */
           @Override
           @Transactional
           public String persist(){
                if(null != getInstance()){
                     Role r = userTreeUtil.getRoleByName(getInstance().getName());
                     if(null != r)
                          throw new UniqueRoleNameValidationException(r.getName());
                }
      
                return super.persist();
           }
      }
      



      The problem is, that whenever a NoResultException is thrown the transaction is marked to be rolled back and I get a javax.persistence.TransactionRequiredException: no transaction is in progress when performing the actual persist operation. Is there any way of handling the NoResultException without causing the current transaction to be rolled back?


      Thanks
      Arno



        • 1. Re: NoResultException causes transaction to rollback

          you can change your Method getRoleByName something like this and based on return value you can perform your operation


          boolean ret = false;
          Role r = (Role)entityManager.createQuery("query").setParameter("parameter",name).getSingleresult();
          
          if(r==null){
          ret = true;
          }
          return ret;



          • 2. Re: NoResultException causes transaction to rollback
            acerberus

            True. But what strikes me is as odd is that I cannot use Seam's Query component since it throws the NoResultException and thereby causes the current transaction to rollback. I would almost go as far as calling this a bug in Seam since the official documentation of NoResultException says:



            Thrown by the persistence provider when getSingleResult() is executed on a query and there is no result to return. This exception will not cause the current transaction, if one is active, to be marked for roll back.

            (see NoResultException JavaDoc)


            So why does Seam ignore this making the Query component almost useless?