EJB are transactional components. The transaction can be managed either by the applicaiton server itself (CMT - container-managed transaction), or manually by yourself within the EJB (BMT - bean-managed transaction).
EJB supports distributed transaction through the JTA specification. The distributed transaction is controlled using UserTransaction
, which has methods begin
, commit
, rollback
.
With CMT, the application server starts, commit and rollback the transaction (according to the transaction annotations) for you and you are not allowed to interfere. This means you must not access the UserTransaction
in this case. However, with BMT, you do that manually and you control the transaction yourself using the UserTransaction
.
Let's move now to the EntityManager
. A JPA implementation can be used either within an application server or stand-alone. If use in stand-alone, you need to use EntityManage.getTransaction
to demarcate the JDBC transaction yourself. If used within an application server, the EntityManager
cooperated with the JTA distributed transaction manager transparently for you.
Most of the time, you use CMT with @Required
annotation on the EJB. This means that you don't need to access neither UserTransaction
nor EntityManager.getTransaction
. The app. server starts and commits the transaction, but also takes care to rollback if an exception is raised. This is what I would recommend for your facade.
(There are more subtleties, such as the PersistenceContextType
or the manual enlistment of the entity manager in distributed transaction with EntityManager.joinTransaction
, but that's only if you use the technologies in a different ways as the default).