How can I catch exceptions from a container-managed transactional transaction?

I have an @StatelessEJB with an interface @WebServiceusing container-managed transactions, which means that transactions are automatically committed by the container after the method is called if it does not throw a system exception.

If I try to execute EntityManager.persist(...)two objects with the same value for a column with a unique constraint on it, the container will call PersistenceExceptionfor the client when committing outside of my code. How can I catch this exception so that I can recover my own application exception?

Do I have to commit a transaction manually in my methods to catch exceptions when committing? (And is there a EntityManager.flush()right way to do this?) If so, then what is the point of container-driven transactions?

+5
source share
1 answer

Unfortunately, it is not possible to exclude exceptions from the failure of a transaction managed by a container. As you stated, the best option is to use a managed transaction bean. Alternatively, you can wrap your EM EJB with a bean proxy that implements the same interface. Container-managed transactions are appropriate if your code does not need to respond to specific crashes.

+3

All Articles