<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Freya.no &#187; mvc</title>
	<atom:link href="http://wp.freya.no/tag/mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://wp.freya.no</link>
	<description>Knowledge is power</description>
	<lastBuildDate>Mon, 06 Feb 2012 19:56:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Spring MVC: how to build a thread-safe Controller</title>
		<link>http://wp.freya.no/2012/02/spring-mvc-how-to-build-a-thread-safe-controller/</link>
		<comments>http://wp.freya.no/2012/02/spring-mvc-how-to-build-a-thread-safe-controller/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 14:24:28 +0000</pubDate>
		<dc:creator>Tatyana</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[safety]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://wp.freya.no/?p=1131</guid>
		<description><![CDATA[Controllers in Spring MVC are desined to be shared between requests. Each controller has a default singleton scope so if you are using controllers you neeed to be aware of that. The easiest way to make sure your controller is thread-safe is to not to have class variables. F.ex. this example from Spring MVC tutorial: [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Controllers in Spring MVC are desined to be shared between requests. Each controller has a default singleton scope so if you are using controllers you neeed to be aware of that. The easiest way to make sure your controller is thread-safe is to not to have class variables. F.ex. this example from <a href="http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html#13.11.13">Spring MVC tutorial</a>:</p>
<pre class="brush: java; title: ; notranslate">
@Controller
@RequestMapping(&quot;/editPet.do&quot;)
public class EditPetForm {

    private final Clinic clinic;

    public EditPetForm(Clinic clinic) {
        this.clinic = clinic;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String setupForm(@RequestParam(&quot;petId&quot;) int petId, ModelMap model) {
        Pet pet = this.clinic.loadPet(petId);
        model.addAttribute(&quot;pet&quot;, pet);
        return &quot;petForm&quot;;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processSubmit(
            @ModelAttribute(&quot;pet&quot;) Pet pet, BindingResult result, SessionStatus status) {

        new PetValidator().validate(pet, result);
        if (result.hasErrors()) {
            return &quot;petForm&quot;;
        }
        else {
            this.clinic.storePet(pet);
            status.setComplete();
            return &quot;redirect:owner.do?ownerId=&quot; + pet.getOwner().getId();
        }
    }
}
</pre>
<p>In this example we can have a serious issue regarding thread-safety: private variable clinic. If there will be a situation where to different requests access the controller simultaniously, one of them will instanciate the controller and begin to process a form, then the other one comes inn. The result from processing of the first request will be sent to the other one, thus the requests receive fail data or nothing.</p>
<p>The solution to the problem may be the following:<br />
1. Annotate Controller with @Scope(&#8220;request&#8221;) or @Scope(&#8220;session&#8221;)<br />
2. Move private variable into one of the methods or save it in session or model.</p>
 <img src="http://wp.freya.no/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1131" width="1" height="1" style="display: none;" /><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwp.freya.no%2F2012%2F02%2Fspring-mvc-how-to-build-a-thread-safe-controller%2F&amp;title=Spring%20MVC%3A%20how%20to%20build%20a%20thread-safe%20Controller" id="wpa2a_6"><img src="http://wp.freya.no/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://wp.freya.no/2012/02/spring-mvc-how-to-build-a-thread-safe-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.net Model View Controller MVC?</title>
		<link>http://wp.freya.no/2009/01/aspnet-model-view-controller-mvc/</link>
		<comments>http://wp.freya.no/2009/01/aspnet-model-view-controller-mvc/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 23:56:22 +0000</pubDate>
		<dc:creator>kent</dc:creator>
				<category><![CDATA[Ikke interessant]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://wp.freya.no/?p=213</guid>
		<description><![CDATA[No shit, it&#8217;s real name is Magic Voodoo Controller. There&#8217;s so much stuff going on behind the scenes and no clear connection between the Model, View and Controller. But it works, somehow. No related posts.
No related posts.]]></description>
			<content:encoded><![CDATA[<p>No shit, it&#8217;s real name is Magic Voodoo Controller.</p>
<p>There&#8217;s so much stuff going on behind the scenes and no clear connection between the Model, View and Controller. But it works, somehow.</p>
 <img src="http://wp.freya.no/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=213" width="1" height="1" style="display: none;" /><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwp.freya.no%2F2009%2F01%2Faspnet-model-view-controller-mvc%2F&amp;title=ASP.net%20Model%20View%20Controller%20MVC%3F" id="wpa2a_14"><img src="http://wp.freya.no/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://wp.freya.no/2009/01/aspnet-model-view-controller-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

