0

If you're using container managed persistence, and you look up a persistence unit by its JNDI name (as defined in ejb-jar.xml in the persistence-context-ref):

(i.e. EntityManager manager = (EntityManager) context.lookup(jndiName); )

Is it an EMF associated with the PU behind the scene pumping out EMs, or is it just a global EM linked to a single thread?

I ask because I want to decouple persistence unit management from my DAOs, and this would be possible if it's a new EM each time, but if it's the same one, and EMs are not thread-safe, then I can't share it between 30 services.

Debugging shows it is an JpaTxEntityManager being returned, which I see is some sort of proxy object, but I can't find anywhere about how the mechanism is actually handled behind the scenes.

Does anyone know? Thanks

Aleksandr M
  • 23,647
  • 12
  • 63
  • 129
djb
  • 1,413
  • 1
  • 16
  • 44
  • Can refer here http://stackoverflow.com/a/4544053/366964 – Nayan Wadekar May 30 '13 at 09:07
  • your documentation refers to application managed persistence unfortunately. Any time you create an EMF you forego container management. – djb May 30 '13 at 10:22
  • If you are looking it up from JNDI, I believe you are probably letting the container place it there, so they are container managed. The code you have shown references an EntityManager, not an EMF. If you get an EMF and use it to obtain EntityManagers, you are responsible for their lifecycle and should not share them. – Chris May 30 '13 at 13:20
  • Yeah, It gives you an EM, not an EMF. It's container managed, and it's giving me an EntityManager. So I want to know what is this EntityManager? Is it a new one, or one that 30 other threads have a reference to? – djb Jun 10 '13 at 13:56

0 Answers0