Transaction Scoped EntityManager

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

In JPA, entity manager can be acquired in different ways. Entity manager acquired using @PersistenceContext annotation is called as container managed entitymanager. Transaction scoped entitymanager are managed by container

Transaction Scoped EntityManager

The lifecycle of entity manager obtained using @PersistenceContext annotation is managed by container itself. Application does not have to create entity manager. Transaction scoped entitymanager is one of the two types of container managed entity managers

Consider an example as below:

Let us understand what happens in this code and how a transaction scoped entitymanager comes into the picture.

  • When find method is invoked, container checks for available transaction.
  • Then it checks for persistence context. As there isn’t one right now, container managed persistence context creates one and finds the project as per id.
  • While searching for employee, it checks the transaction again and finds one which was created to find project.
  • Now both entities are managed and entity manager makes the change while committing.
  • Entity manager goes away when transaction is over.
  • This process is repeated every time whenever one or more entity managers operations are invoked within a transaction.

I hope this helped understand the concept of transaction scoped entitymanager.

References for further study :

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

3 thoughts on “Transaction Scoped EntityManager

Leave a Reply

Your email address will not be published. Required fields are marked *