<?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 the Memento pattern to solve thread safety issues</title>
	<atom:link href="http://www.gridshore.nl/2008/12/27/using-the-memento-pattern-to-solve-thread-safety-issues/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gridshore.nl/2008/12/27/using-the-memento-pattern-to-solve-thread-safety-issues/</link>
	<description>A weblog about software engineering, Architecture, Technology an other things we like.</description>
	<lastBuildDate>Thu, 29 Jul 2010 07:45:23 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Links for the day - 25-01-2009 &#124; Blogmines</title>
		<link>http://www.gridshore.nl/2008/12/27/using-the-memento-pattern-to-solve-thread-safety-issues/comment-page-1/#comment-14277</link>
		<dc:creator>Links for the day - 25-01-2009 &#124; Blogmines</dc:creator>
		<pubDate>Sun, 25 Jan 2009 17:15:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.gridshore.nl/2008/12/27/using-the-memento-pattern-to-solve-thread-safety-issues/#comment-14277</guid>
		<description>[...] Using the Memento pattern to solve thread safety issues [...]</description>
		<content:encoded><![CDATA[<p>[...] Using the Memento pattern to solve thread safety issues [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://www.gridshore.nl/2008/12/27/using-the-memento-pattern-to-solve-thread-safety-issues/comment-page-1/#comment-11452</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Sun, 04 Jan 2009 02:26:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.gridshore.nl/2008/12/27/using-the-memento-pattern-to-solve-thread-safety-issues/#comment-11452</guid>
		<description>Hi Gerbrand,

Long time no see! How are you doing? Is the new job going well?

You are correct &#8212; the example given in the post is amenable to all of its fields being final (although it makes little difference since they are private and there are no setters). The main reason I didn&#039;t make them final here is that the example is adapted from the code we really developed and that was slightly different, making final fields unusable.

The &quot;prototype&quot; scope might be a solution depending on how your application was built. The main thing to remember is that prototype scope means that a bean is instantiated every time it is referenced FROM THE SPRING CONTEXT, not every time it is used from client code. That is, every time a bean is injected, a new object will be created for injection; but injected is injected so if some code USES an injected (prototype) bean twice, that client code will not magically be given a new bean object between Java method calls (unless the client code retrieves the bean from the context itself before each method call on that bean). Particularly relevant here is that a prototype bean injected into a singleton bean will not be replaced in the lifetime of the singleton bean. In a typical web or webservice app you would not have the entire bean tree rebuilt for each request (you would make the middle tier service beans singletons), so prototype scope will not help. Unless you make EVERY bean in the middle tier (or at least every bean involved with a certain request-response) prototype scoped, but then you have to consider carefully what the impact will be on the performance of the application. Or you can use method injection to inject a lookup of the prototype bean into the unmodified (singleton) service code you already had, but that probably isn&#039;t the &lt;em&gt;quick&lt;/em&gt; fix I was looking for in this article.

Note that you &lt;em&gt;can&lt;/em&gt; possibly fix the entire situation in the Spring context definition in a web application by using the request scope rather than the prototype scope (this has a bean recreated each time an HTTP request is received). Your Spring config will become a bit more fiddly though, since you&#039;ll need an AOP scope proxy to make sure that the request-scoped bean is correctly re-injected (or looked up) in the existing service code. I have to note though that I&#039;m not completely sure of how thread-safe this solution is. Plus, I have a hunch that if your code is amenable to this solution, you were probably already clever enough as a developer to avoid the problem I was trying to fix. ;-)</description>
		<content:encoded><![CDATA[<p>Hi Gerbrand,</p>
<p>Long time no see! How are you doing? Is the new job going well?</p>
<p>You are correct &mdash; the example given in the post is amenable to all of its fields being final (although it makes little difference since they are private and there are no setters). The main reason I didn&#8217;t make them final here is that the example is adapted from the code we really developed and that was slightly different, making final fields unusable.</p>
<p>The &#8220;prototype&#8221; scope might be a solution depending on how your application was built. The main thing to remember is that prototype scope means that a bean is instantiated every time it is referenced FROM THE SPRING CONTEXT, not every time it is used from client code. That is, every time a bean is injected, a new object will be created for injection; but injected is injected so if some code USES an injected (prototype) bean twice, that client code will not magically be given a new bean object between Java method calls (unless the client code retrieves the bean from the context itself before each method call on that bean). Particularly relevant here is that a prototype bean injected into a singleton bean will not be replaced in the lifetime of the singleton bean. In a typical web or webservice app you would not have the entire bean tree rebuilt for each request (you would make the middle tier service beans singletons), so prototype scope will not help. Unless you make EVERY bean in the middle tier (or at least every bean involved with a certain request-response) prototype scoped, but then you have to consider carefully what the impact will be on the performance of the application. Or you can use method injection to inject a lookup of the prototype bean into the unmodified (singleton) service code you already had, but that probably isn&#8217;t the <em>quick</em> fix I was looking for in this article.</p>
<p>Note that you <em>can</em> possibly fix the entire situation in the Spring context definition in a web application by using the request scope rather than the prototype scope (this has a bean recreated each time an HTTP request is received). Your Spring config will become a bit more fiddly though, since you&#8217;ll need an AOP scope proxy to make sure that the request-scoped bean is correctly re-injected (or looked up) in the existing service code. I have to note though that I&#8217;m not completely sure of how thread-safe this solution is. Plus, I have a hunch that if your code is amenable to this solution, you were probably already clever enough as a developer to avoid the problem I was trying to fix. <img src='http://www.gridshore.nl/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gerbrand van Dieijen</title>
		<link>http://www.gridshore.nl/2008/12/27/using-the-memento-pattern-to-solve-thread-safety-issues/comment-page-1/#comment-11439</link>
		<dc:creator>Gerbrand van Dieijen</dc:creator>
		<pubDate>Sat, 03 Jan 2009 16:28:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.gridshore.nl/2008/12/27/using-the-memento-pattern-to-solve-thread-safety-issues/#comment-11439</guid>
		<description>Hi Ben,

Avoiding objects with (changing) state is usually a lot more efficient then having objects with modifiable state, so I think your listed solution sounds good.

I suggest adding final to all fields in ServiceMemento, do indicate the state of the object should not be changed.

Would a quick alternative solution not be changing the scope of the bean in the spring configuration?
By changing the scope to prototype (http://static.springframework.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes-prototype) the object should be instantiated each time it&#039;s referenced.</description>
		<content:encoded><![CDATA[<p>Hi Ben,</p>
<p>Avoiding objects with (changing) state is usually a lot more efficient then having objects with modifiable state, so I think your listed solution sounds good.</p>
<p>I suggest adding final to all fields in ServiceMemento, do indicate the state of the object should not be changed.</p>
<p>Would a quick alternative solution not be changing the scope of the bean in the spring configuration?<br />
By changing the scope to prototype (<a href="http://static.springframework.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes-prototype" rel="nofollow">http://static.springframework.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes-prototype</a>) the object should be instantiated each time it&#8217;s referenced.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
