File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EntityManager in Servlets Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EntityManager in Servlets Doubt" Watch "EntityManager in Servlets Doubt" New topic
Author

EntityManager in Servlets Doubt

nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
These are the two code snippets given in EJB 3 in Action on page 448 and page 449:


//--- Do NOT do this!!! This is not recommended!!!
public class ActionBazaarBidControllerServlet extends HttpServlet{
@PersistenceContext(unitName="actionBazaar")
private EntityManager entityManager;
}


and


@PersistenceContext(name="actionBazaar/EntityManager", unitName="actionBazaar")
public class ActionBazaarBidControllerServlet extends HttpServlet{
private EntityManager entityManager;
}


I am not able to understand how these two are different. I know that the Servlet is a multi threaded environment and EntityManager is not thread safe, but how is the 2nd code snippet any different from the first one?



Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

In the first example you are injecting the PersistenceContext into your servlet's entityManager instance variable.

In the second one, you are registering into the JNDI ENC a reference to the PersistenceContext under the "actionBazaar/EntityManager" name. That registration is done for your servlet's ENC registry only. Your servlet may now do a JNDI lookup of the PersistenceContext using that name.

The second example is doing a JNDI registration while the first one is injecting the PersistenceContext into a member variable of your servlet.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I agree with Sergio. The second example is lacking the JNDI lookup. There is the same explanation somewhere in another chapter of the book. I don't have the book under hands now, so I can't tell you which page it is. Look for a gray background enclosed note in a former chapter.


[My Blog]
All roads lead to JavaRanch
Jim Jacob
Greenhorn

Joined: May 27, 2008
Posts: 8
Page : 307


[SCJP,SCWCD,SCBCD]
nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
Ok. I can understand that guys, but now my question is, does doing a JNDI lookup in the second code snippet make the EntityManager thread safe? Doesn't it too have the same disadvantages as a injection has?
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by nitin pai:
Ok. I can understand that guys, but now my question is, does doing a JNDI lookup in the second code snippet make the EntityManager thread safe?


It does. The lookup is done inside your servlet's service method: once for each thread. That means, each thread will have its own PersistenceContext instance.

In the other hand, when you inject the PersitenceContext into your servlet's member variable you'll end with only one PersistenceContext instance for all the threads your servlet's service method runs on. It is in that kind of scenarios that race conditions may arise.
Leon Omk
Ranch Hand

Joined: Aug 17, 2010
Posts: 75

Sergio Tridente wrote:
Originally posted by nitin pai:
It does. The lookup is done inside your servlet's service method: once for each thread. That means, each thread will have its own PersistenceContext instance.



Personally, I hope it's sure. But I can't prove it myself. Could you provide detailed description from EJB, Jpa or any specification?


OK, so that other guy knows Java better than I do, but I bet he can't speak Wuhanese(a Chinese Dialect) like me.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EntityManager in Servlets Doubt
 
Similar Threads
Got NullPointer exception when injecting EntityManger
Obtaining a PersistenceContext in Java EE
[JPA 2.0] Redoploying app deletes my database?
Apache Derby Sql Statement's
JPA2.0 CascadeType.ALL doesn't cascade persist