user3909622
user3909622

Reputation:

Cant catch exception in EJB

I am building a restful webservice using Jax-RS and EJB3 running on wildfly 8. The system is splited into two parts. A rest module and a persistence module. The rest module consists of a rest servlet with a method, reacheable under "mycontext/rest/do". This method calls the method "do()" of a stateless bean which represents the interface of the persistence module. "do()" calls "persistEntity(Entity e)" of another stateless bean which represents a facade (kind of an interface pattern) of a entity class. The entity I want to persist has a few constraints. If they get violated an exception like "ConstraintViolationException" gets thrown (by the EJB container I think). I try to catch the exception in one of the two stateless beans in the persistence module but this is not working. That means that the code in the catch block is not executed. But catching the exception in the servlet works.

Rest servlet (catching exception works here):

@Path("/rest")
public class AccountRestService {
    @Inject
    private Service service;
    @POST
    public void do(Entity entity) {
        service.do(entity);
    }
}

Bean representing interface of the persistence module (catching exception is ignored):

 @Stateless
 @LocalBean
 public class Service {
     @Inject
     private EntityFacade facade;
     public void do(Entity entity) {
         facade.persist(entity);
     }
 }

Entity Facade (catching exception is ignored):

@Stateless
public class EntityFacade{
    @PersistenceContext(unitName = "primary")
    private EntityManager em;
    public void create(Entity entity){
        em.persist(entity); // this line throws exception like ConstraintViolationException
    }
}

Upvotes: 1

Views: 1293

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

That's expected. The exception is thrown at flush time. The flush happens at transaction-commit time. And the commit happens after the EJB method has returned successfully.

So, contrary to what you say, it's not the persist() method which throws the exception (the stack trace would confirm that), but the method of the transactional proxy which wraps your EJB and commits the transaction.

There is really nothing you can do with such an exception except

  • show an error message to the user. In that case, catching the exception in the rest service, or letting a generic exception handler handle it is the right thing to do
  • retry the transaction. In that case, you should have an intermediate layer (or method) between the rest service method and the EJB, which calls the transactional EJB, catches the exception and retries.

Upvotes: 4

Related Questions