I've got a follow-up question to: EJB 3.1 container managed concurrency vs. synchronized
Is a Lock only preventing concurrent access to data within the persistence context, etc. or is it also providing synchronization for private fields?
I've got a follow-up question to: EJB 3.1 container managed concurrency vs. synchronized
Is a Lock only preventing concurrent access to data within the persistence context, etc. or is it also providing synchronization for private fields?
It's easiest to imagine that there is a per-bean java.util.concurrent.ReadWriteLock
on which the container is calling lock()
and unlock()
around the method call on either the readLock()
or writeLock()
, depending on the configured @Lock
for the method. So, instance variables are protected.
However, be aware that a container-managed @PersistenceContext
field is not "shared" state because the container actually injects a proxy object. Each method call on that proxy EntityManager
will delegate to a per-transaction EntityManager
. Since transactions are not shared across threads, it is thread-safe even if the bean is using @ConcurrencyManagement(BEAN)
with no other synchronization.