<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Using ThreadLocal and Servlet Filters to cleanly access JPA an EntityManager</title>
	<atom:link href="http://www.naildrivin5.com/daveblog5000/?feed=rss2&#038;p=39" rel="self" type="application/rss+xml" />
	<link>http://www.naildrivin5.com/daveblog5000/?p=39</link>
	<description>Stuff that happens to Dave</description>
	<lastBuildDate>Tue, 11 Aug 2009 20:09:24 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Effective using Java Concurrency part-1 &#171; When IE meets SE&#8230;</title>
		<link>http://www.naildrivin5.com/daveblog5000/?p=39&#038;cpage=1#comment-70</link>
		<dc:creator>Effective using Java Concurrency part-1 &#171; When IE meets SE&#8230;</dc:creator>
		<pubDate>Sat, 08 Aug 2009 22:30:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.naildrivin5.com/daveblog5000/?p=39#comment-70</guid>
		<description>[...] Thread-Local Variables In Java Threading lightly, Part 3: Sometimes it&#8217;s best not to share Using ThreadLocal and Servlet Filters to cleanly access JPA an EntityManager Thread-local variables in Java Introduction to reducing thread contention in Java Possibly related [...]</description>
		<content:encoded><![CDATA[<p>[...] Thread-Local Variables In Java Threading lightly, Part 3: Sometimes it&#8217;s best not to share Using ThreadLocal and Servlet Filters to cleanly access JPA an EntityManager Thread-local variables in Java Introduction to reducing thread contention in Java Possibly related [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jaxent</title>
		<link>http://www.naildrivin5.com/daveblog5000/?p=39&#038;cpage=1#comment-69</link>
		<dc:creator>jaxent</dc:creator>
		<pubDate>Tue, 28 Jul 2009 17:24:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.naildrivin5.com/daveblog5000/?p=39#comment-69</guid>
		<description>Thanks for posting that.  It was one of those, &quot;Why didn&#039;t I think of that?&quot; moments.  Saves me a lot time and resource leak potential.</description>
		<content:encoded><![CDATA[<p>Thanks for posting that.  It was one of those, &#8220;Why didn&#8217;t I think of that?&#8221; moments.  Saves me a lot time and resource leak potential.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mbinette</title>
		<link>http://www.naildrivin5.com/daveblog5000/?p=39&#038;cpage=1#comment-34</link>
		<dc:creator>mbinette</dc:creator>
		<pubDate>Sun, 14 Sep 2008 18:54:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.naildrivin5.com/daveblog5000/?p=39#comment-34</guid>
		<description>When I said I create the EntityManager once instead of for each request, I meant to add that it is per thread using ThreadLocal like you have done.  So all requests on the same thread will use the same EntityManager but requests on another thread will use a different EntityManager.  

I didn&#039;t use a Filter because a lot of my requests don&#039;t need an EntityManager.  I hadn&#039;t thought of the automatic rollback.  Right now we catch the exception in our &quot;service&quot; code and don&#039;t have a generic catch all to perform that action.   That is a big benefit to using a Filter like you have done.</description>
		<content:encoded><![CDATA[<p>When I said I create the EntityManager once instead of for each request, I meant to add that it is per thread using ThreadLocal like you have done.  So all requests on the same thread will use the same EntityManager but requests on another thread will use a different EntityManager.  </p>
<p>I didn&#8217;t use a Filter because a lot of my requests don&#8217;t need an EntityManager.  I hadn&#8217;t thought of the automatic rollback.  Right now we catch the exception in our &#8220;service&#8221; code and don&#8217;t have a generic catch all to perform that action.   That is a big benefit to using a Filter like you have done.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://www.naildrivin5.com/daveblog5000/?p=39&#038;cpage=1#comment-33</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Fri, 12 Sep 2008 16:43:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.naildrivin5.com/daveblog5000/?p=39#comment-33</guid>
		<description>My understanding of how EntityManager works is that you need one per transaction; if all requests were using the same instance, a rollback from one thread would rollback all threads.

I&#039;m not sure how this works in EJB3.0, but it seems to insert one for each EJB transaction.  The docs for Hibernate recommended this approach, but it may not be the only one.

Also, ServletContextListener doesn&#039;t allow you to examine the results of the servlet call; in my case, any exception thrown by the servlet call results in a rollback, this way my code doesn&#039;t have to worry about committing or rolling back; it can just proceed as normal.  If we ever went to full-blown EJB3, not much of the code would need to change.</description>
		<content:encoded><![CDATA[<p>My understanding of how EntityManager works is that you need one per transaction; if all requests were using the same instance, a rollback from one thread would rollback all threads.</p>
<p>I&#8217;m not sure how this works in EJB3.0, but it seems to insert one for each EJB transaction.  The docs for Hibernate recommended this approach, but it may not be the only one.</p>
<p>Also, ServletContextListener doesn&#8217;t allow you to examine the results of the servlet call; in my case, any exception thrown by the servlet call results in a rollback, this way my code doesn&#8217;t have to worry about committing or rolling back; it can just proceed as normal.  If we ever went to full-blown EJB3, not much of the code would need to change.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mbinette</title>
		<link>http://www.naildrivin5.com/daveblog5000/?p=39&#038;cpage=1#comment-32</link>
		<dc:creator>mbinette</dc:creator>
		<pubDate>Thu, 11 Sep 2008 21:16:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.naildrivin5.com/daveblog5000/?p=39#comment-32</guid>
		<description>Is there a reason you didn&#039;t use a ServletContextListener instead of a filter?

I am using a solution that works similarly but only creates the EntityManager once instead of for each request.  Is there a reason why you would want to close the EntityManager after every request?</description>
		<content:encoded><![CDATA[<p>Is there a reason you didn&#8217;t use a ServletContextListener instead of a filter?</p>
<p>I am using a solution that works similarly but only creates the EntityManager once instead of for each request.  Is there a reason why you would want to close the EntityManager after every request?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
