• Post Reply Bookmark Topic Watch Topic
  • New Topic

How is EntityManager thread-safe in Session Beans?

 
Anne Forumer
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is true that only a single thread runs through a session bean. But isn't the entityManager injected into all session beans the same instance bound into JNDI? If this is true, aren't multiple threads, one for each session bean, running through the one instance of entityManager?

I must be missing something! I'd appreciate somebody setting me up straight on this. I'd appreciate any references on this subject. Thanks!
 
Pallav Bora
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Hibernate doc:

An EntityManager is an inexpensive, non-threadsafe object that should be used once, for a single business process, a single unit of work, and then discarded. An EntityManager will not obtain a JDBC Connection (or a Datasource) unless it is needed, so you may safely open and close an EntityManager even if you are not sure that data access will be needed to serve a particular request
 
Anne Forumer
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pallav, but this doesn't answer my question.

Two different session beans may have obtained the same EntityManager and processing calls simultaneously. Unless each Session Bean receives a new EntityManager, it could cause problems - the way I see it. But I'm open to be corrected.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!