Binders Lachel
Binders Lachel

Reputation: 635

How does an unsynchronized EntityManager read from DB?

I am learning EJB/JPA and I wonder how should this snippet work:

@Stateful
public class Sample {
   @PersistenceContext(type=PersistenceContextType.EXTENDED, 
                       synchronization=SynchronizationType.UNSYNCHRONIZED,
                       unitName="..")
   EntityManager em;

   public Employee get(int id){
      retirm em.find(Employee.class , id);      
   }

   // ...
}

It's a stateful bean, so by default it should be wrapped in container manager transaction (REQUIRED).

But on the other hand an unsynchronized manager is said to NOT join a transaction no matter what unless explicitly joined.

So, if it doesn't join a tx, how does it fetch data from DB? Is it possible it will contain dirty/phantom reads etc?

This snippet is from "Pro JPA 2".

Upvotes: 0

Views: 509

Answers (1)

zbig
zbig

Reputation: 3966

From the 3.1.1 of JPA 2.1 spec:

The find method (provided it is invoked without a lock or invoked with LockModeType.NONE) and the getReference method are not required to be invoked within a transaction. If an entity manager with transaction-scoped persistence context is in use, the resulting entities will be detached; if an entity manager with an extended persistence context is used, they will be managed.

Upvotes: 2

Related Questions