Wednesday, March 16, 2011

EntityManager must be access within a transaction

The error means of course
"EntityManager must be accessED within a transaction"

annotating your EJB method with:
@TransactionAttribute(TransactionAttributeType.REQUIRED)

(which is the default), ensures that
"If the client is not associated with a transaction, the container starts a new transaction before running the method."

(see here)
( see also discussion here)

Using the
em.joinTransaction();


you can annotate your method with
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
to make sure you join a brand new transaction

No comments: