1

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?

Community
  • 1
  • 1
D.R.
  • 20,268
  • 21
  • 102
  • 205

1 Answers1

1

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.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90