Raf Szczypiorski wrote:Persist and a few other method must always be called within a transaction. For aplication-managed entity managers that are resource-local, the transaction is EntityTransaction. Yes, it is mandatory here.
Confused with that. Lifetime of the PersistenceContext, on an application-managed EntityManager should be handled manually by using EntityManager.close() method. Am I correct?
If it is, I couldn't figure out that why it is mandatory to begin a transaction before calling to the persist() method. Does it always need an active transaction? But, for what?
Please help on this.
Amandeep Singh wrote:
Yes persist() always need a active transaction. Otherwise it would throw transaction required exception.
JavaDoc wrote:TransactionRequiredException will be thrown if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.
Treimin Clark wrote:
Amandeep Singh wrote:
Yes persist() always need a active transaction. Otherwise it would throw transaction required exception.
The JavaDoc says that
JavaDoc wrote:TransactionRequiredException will be thrown if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.
In that case, an extended persistence context doesn't require an active transaction. So, why application-managed EntityManager do require it?
When an EntityManager with an extended persistence context is used, the persist, remove, merge, and
refresh operations may be called regardless of whether a transaction is active. The effects of these oper-
ations will be committed to the database when the extended persistence context is enlisted in a transac-
tion and the transaction commits.
The scope of the persistence context of an application-managed entity manager is extended. It is the
responsibility of the application to manage the lifecycle of the persistence context.