Search Options
close
Search the following clips:
All Clips
Everyone's Clips
My Guides
Sign Up
Install
Learn More
Login
EntityManger & EntityManagerFactory
baljinder
follow
0
8-29-2008 11:23 AM
52 views
Add a Comment
Login
to Comment. Not a member yet?
Sign up
Related Clips
Three Reasons Cash is King
Urban Farming School Takes Root
Pentagon reserves troops for domestic emer...
Design for Cheaper Wind Power
Pentagon wants to build ethical soldier bots
Farming And Chemical Warfare: A Day In The...
New York Senate may not take up gay marria...
More clips from
baljinder
RowSet & Data Provider in Netbeans
Persisntence & JNDI
Programming language types 2
Today's Top Clips
Humanity May Hold Key For Next Earth Evolution
New Dolphin Species Discovered
The 'morning after' HIV drug cocktail
Denmark's Kinder, Gentler System of Eugenics
What will happen in the next 5 years? read it here :)
"No Child Left Behind" a "Trojan Horse" for Pentagon Recruiters
Farming And Chemical Warfare: A Day In The Life Of An Ant
Press and "Psy Ops" to Merge
Why do people overbid in auctions?
After Seven Years, Guantánamo Prisoners Need to be Tried or Released
visit the
Top Clips page
View the Top Clips from
August 29, 2008
Embed This Clip In Your Site...
<div style="margin: 12px 0px; font-family: arial; color: #333333; background: #ffffff; border: solid 4px #e5e5e5; width: 100%; clear: left;"><div class="CM_CTB_Content_Wrap" style="margin: 0px; padding: 0px;background-color: #ffffff;"><div style="border-bottom: solid 1px #dcdcdc; white-space: nowrap; margin-bottom: 8px; background-color: #eeeeee ;background-image: url(http://clipmarks.com/images/source-bg.gif); background-repeat: repeat-x; height: 24px; line-height: 24px; vertical-align: middle; padding-bottom: 4px; color: #666666; font-size: 10px;" ><a href="http://clipmarks.com/clip-to-blog/" title="see clips that are hot right now"><img src="http://content.clipmarks.com/blog_embed/a6f473bf-8f39-476d-9bfd-c40b342e459e/208D9785-B27F-4595-A34E-8AE858075320/" alt="" width="19" height="19" border="0" style="vertical-align: middle; margin: 0px 4px; display: inline; border: none; float:none;" /></a>clipped from <a title="http://javahowto.blogspot.com/2007/10/mind-thread-safety-when-injecting.html" href="http://javahowto.blogspot.com/2007/10/mind-thread-safety-when-injecting.html" style="font-size: 11px;">javahowto.blogspot.com</a></div><blockquote style="text-align: left; padding: 0px 8px; margin: 4px 0px 8px 0px; background: transparent; border: none;" cite="http://javahowto.blogspot.com/2007/10/mind-thread-safety-when-injecting.html"><P><DIV>As I wrote in post <A href="http://javahowto.blogspot.com/2007/10/why-we-need-type-level-injections-in.html">Why we need type-level injections in JavaEE</A>, injecting EJB 3 stateful beans into servlet instance fields is not thread-safe. Along the same line, injecting <CODE>EntityManager</CODE> with <CODE>@PersistenceContext</CODE> into servlet instance variables is not thread-safe, either. <CODE>EntityManager</CODE> is just not designed to be thread-safe.</DIV><BR />For example, the following code snippet of a servlet class is incorrect:</P></blockquote><div style="height: 2px; font-size: 2px; background: #dcdcdc; border-bottom: solid 1px #f5f5f5; margin: 2px 4px;"></div><blockquote style="text-align: left; padding: 0px 8px; margin: 4px 0px 8px 0px; background: transparent; border: none;" cite="http://javahowto.blogspot.com/2007/10/mind-thread-safety-when-injecting.html"><CODE>public class FooServlet extends HttpServlet {<BR /> //This field injection is not thread-safe.<BR /> //FIXME<BR /> @PersistenceContext<BR /> private EntityManager em;<BR />...<BR />}</CODE></blockquote><div style="height: 2px; font-size: 2px; background: #dcdcdc; border-bottom: solid 1px #f5f5f5; margin: 2px 4px;"></div><blockquote style="text-align: left; padding: 0px 8px; margin: 4px 0px 8px 0px; background: transparent; border: none;" cite="http://javahowto.blogspot.com/2007/10/mind-thread-safety-when-injecting.html">One way to fix this is to inject <CODE>EntityManagerFactory</CODE> instead. <CODE>EntityManagerFactory</CODE> is guaranteed to be thread-safe. For example:</blockquote><div style="height: 2px; font-size: 2px; background: #dcdcdc; border-bottom: solid 1px #f5f5f5; margin: 2px 4px;"></div><blockquote style="text-align: left; padding: 0px 8px; margin: 4px 0px 8px 0px; background: transparent; border: none;" cite="http://javahowto.blogspot.com/2007/10/mind-thread-safety-when-injecting.html"><CODE>public class FooServlet extends HttpServlet {<BR /> //This field injection is thread-safe<BR /> @PersistenceUnit<BR /><DIV> private EntityManagerFactory emf;</DIV><BR /> protected void doGet(HttpServletRequest request,<BR /> HttpServletResponse response)<BR /> throws ServletException, IOException {<BR /> EntityManager em = emf.createEntityManager();<BR /> //work with em<BR />}<BR />}</CODE></blockquote></div><div style="margin: 0px 6px 6px 4px;"><table style="font-size: 11px;border-spacing: 0px;padding: 0px;" cellpadding="0" cellspacing="0" width="100%"><tr><td style="background:transparent;border-width:0px;padding:0px;"> </td><td align="right" style="background:transparent;border-width:0px;padding:0px;width:107px" width="107"><a href="http://clipmarks.com/share/208D9785-B27F-4595-A34E-8AE858075320/blog/" title="blog or email this clip"><img src="http://content7.clipmarks.com/images/c2b-foot.png" border="0" alt="blog it" width="107" height="17" style="border-width:0px;padding:0px;margin:0px;" /></a></td></tr></table></div></div>
Clipmarks
Home
New Clips
Top Clips
Dashboard
Popular Topics
News
Life
Science
Technology
Entertainment
Get Started
Sign Up
Install Clipping Tool
How Clipping Works
Clip-to-Blog™
ClipSearch
Tools and Resources
FAQ
ClipWeek
Top Clippers
Top Tags
Site Map
About Clipmarks
About Us
Contact
Blog
Copyright
Privacy
EULA
OK