2 Replies Latest reply on Sep 4, 2008 1:11 PM by leeth

    Catching NoResultException

    leeth

      Hi I have a slsb as a DAO for an entity. I use this slsb i a business component. 
      I would like to catch NonUniqueException or NoResultException there because in my opion this is where I wants to determine what action that need to be taken if these exceptions are thrown. But I cannot catch them directly because they are wrapped around a EJBTransactionRolledbackException. I don't quite understand why this is done? Is it a bug or are there some good reason why PersistenceExceptions are caught and throw new EJBTransactionRolledbackExceptions

        • 1. Re: Catching NoResultException
          blabno

          As to NonUniqueException ... it is thrown when transaction is being commited which does not happen right on em.persist or em.merge but later (i.e. method end). If I want to catch such errors I put em.flush right after em.persist and all exceptions, if any, are thrown there.

          • 2. Re: Catching NoResultException
            leeth

            I don't understand that. Because I dont commit anything, I'm just quering. I have writen a small pseudo example below to illustratede the situation.
            This pseudo example won't work because the dao doesn't throw any of the subtype of PersistenceExceptions but a EJBTransactionRolledbackExceptions.



            try{
            MyEntity myEntity = dao.findActive();
            handleActive(myEntity);
            
            }catch(NoResultException e){
                handleNoActive();
            }catch(NonUniqueResultException e){
                log.fatal("more than one active");
                throw new IllegalStateException("Internal error: more than one active record");
            }
            
            
            
            ....
            public MyEntity findActive(){
                String queryString = "from entity where endDate is not null";
                Query query = em.createQuery(queryString);
                return query.getSingleResult();
            }