5 Replies Latest reply on Jun 6, 2006 3:17 AM by jc7442

    EntityManager and exceptions

    tzablock

      Here is what I would like to do:

      This is my bussiness interface:

      package pl.com.filmservice.ejb;
      
      import java.sql.Date;
      import java.util.List;
      import pl.com.filmservice.par.*;
      
      public interface FilmService
      {
       ...
       public void createUser(User user);
       ...
      }


      and the implementing bean:

      package pl.com.filmservice.ejb;
      
      import java.sql.Date;
      import java.util.List;
      import java.util.ArrayList;
      import javax.ejb.*;
      import javax.persistence.*;
      import pl.com.filmservice.par.*;
      
      @Stateless
      public class FilmServiceBean implements FilmService
      {
       @PersistenceContext(unitName="FSWise")
       EntityManager em;
       ...
       public void createUser(User user)
       {
       em.persist(user);
       }
       ...
      }


      What I would like to do is to check if the em.persist(user) threw a pk constraint violation exception, catch it and throw my own exception class so that I can cathc it later on in the application.

      The problem is that the transaction ends when the createUser method ends and then the exception is thrown. I would like to catch it just after persist() invocation.

      I've tried to use EntityManager.createTransaction(), begin the transaction and commit it just after the persist, but then i get the illegalStateException (i can't call beginTransaction on the managed, injected EntityManager).
      Any ideas about how to solve the problem?

        • 1. Re: EntityManager and exceptions
          tzablock

          Ok maybe I'll specify the question in a different way:

          How can I use a non-injected entity manager?

          • 2. Re: EntityManager and exceptions
            ana_oleski

            Hi,

            you could try the flush() method in the entityManager. It sends the sql to the database right away, that should generate your exception.
            Note that this has no influence on commit, you can still use container-managed transactions.

            Ana

            • 3. Re: EntityManager and exceptions
              elponderador

              Whether your EntityManager is injected or not has nothing to do with your ability to catch an exception.

              You can do this:

              try {
               em.persist(user);
              } catch (Throwable t) {
               if (t instanceof TheExceptionIAmWantToHandleLaterException) {
               ...
               } else {
               ...
               }
              }
              


              • 4. Re: EntityManager and exceptions
                elponderador

                Sorry forgot to include this (this would go in the implementation class):

                public void createUser (...) {
                 try { this.createUserImpl(...); }
                 catch { ... }
                }
                
                private void createUserImpl (...) {
                 em.persist(user);
                }
                


                • 5. Re: EntityManager and exceptions
                  jc7442

                  In your createUser method you could catch the exception and throw a new Exception (with the annotation ApplicationException with the rollback set to false).

                  Exception handling is describe in chapter 14 of the EJB core specification