<?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"
	>
<channel>
	<title>Comments on: ActiveRecord receives pessimistic locking</title>
	<atom:link href="http://www.sitepoint.com/blogs/2006/06/21/activerecord-receives-pessimistic-locking/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs/2006/06/21/activerecord-receives-pessimistic-locking/</link>
	<description>News, opinion, and fresh thinking for web developers and designers. The official podcast of sitepoint.com.</description>
	<pubDate>Fri, 21 Nov 2008 10:42:04 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: ian white</title>
		<link>http://www.sitepoint.com/blogs/2006/06/21/activerecord-receives-pessimistic-locking/#comment-122384</link>
		<dc:creator>ian white</dc:creator>
		<pubDate>Mon, 11 Dec 2006 11:58:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1588#comment-122384</guid>
		<description>Thanks for the writeup!

I've written up some notes on testing rails code with concurrency issues over &lt;a href="http://blog.ardes.com/articles/2006/12/11/testing-concurrency-in-rails-using-fork" rel="nofollow"&gt;here&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>Thanks for the writeup!</p>
<p>I&#8217;ve written up some notes on testing rails code with concurrency issues over <a href="http://blog.ardes.com/articles/2006/12/11/testing-concurrency-in-rails-using-fork" rel="nofollow">here</a></p>]]></content:encoded>
	</item>
	<item>
		<title>By: ritukamthan</title>
		<link>http://www.sitepoint.com/blogs/2006/06/21/activerecord-receives-pessimistic-locking/#comment-40383</link>
		<dc:creator>ritukamthan</dc:creator>
		<pubDate>Tue, 25 Jul 2006 09:57:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1588#comment-40383</guid>
		<description>hey Tim dats a gud article for quick reference. thnx :)</description>
		<content:encoded><![CDATA[<p>hey Tim dats a gud article for quick reference. thnx :)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: timlucas</title>
		<link>http://www.sitepoint.com/blogs/2006/06/21/activerecord-receives-pessimistic-locking/#comment-33864</link>
		<dc:creator>timlucas</dc:creator>
		<pubDate>Thu, 29 Jun 2006 02:14:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1588#comment-33864</guid>
		<description>Thanks Luke, you're totally right!

Not that it really matters in the above example, because we are locking the entire row, but I'll update the article for correctness.</description>
		<content:encoded><![CDATA[<p>Thanks Luke, you&#8217;re totally right!</p>
<p>Not that it really matters in the above example, because we are locking the entire row, but I&#8217;ll update the article for correctness.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Luke Redpath</title>
		<link>http://www.sitepoint.com/blogs/2006/06/21/activerecord-receives-pessimistic-locking/#comment-33652</link>
		<dc:creator>Luke Redpath</dc:creator>
		<pubDate>Wed, 28 Jun 2006 09:31:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1588#comment-33652</guid>
		<description>Tim, there is a major error in your code above. Calling update_attribute will NOT update a single attribute - it will update the single attribute in memory then save the ENTIRE record.</description>
		<content:encoded><![CDATA[<p>Tim, there is a major error in your code above. Calling update_attribute will NOT update a single attribute - it will update the single attribute in memory then save the ENTIRE record.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: timlucas</title>
		<link>http://www.sitepoint.com/blogs/2006/06/21/activerecord-receives-pessimistic-locking/#comment-32079</link>
		<dc:creator>timlucas</dc:creator>
		<pubDate>Thu, 22 Jun 2006 23:17:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1588#comment-32079</guid>
		<description>Sorry SRTech, a full explanation would have been much more helpful.

Read locks such as this ensure that no other queries can read that table row until the transaction has completed. Because you want to be able to execute your code simultaneously, and from different servers, database locks can be used to make sure your is consistent.

For example, say you want to implement something that tracks the visits for your user accounts.

A simple implementation could be:
&lt;pre&gt;&lt;code class="ruby"&gt;
user = User.find(session[:user_id])
user.update_attribute(:visits, user.visits+1)
&lt;/code&gt;&lt;/pre&gt;

If the user makes simultaneous requests you can get what's called a &lt;a href="http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/avoid-race.html" rel="nofollow"&gt;race condition&lt;/a&gt;.

For example, the following could be the order of execution if the the same user made 2 simultaneous requests:
&lt;pre&gt;&lt;code class="ruby"&gt;
user_request_1 = User.find(session[:user_id])
user_request_2 = User.find(session[:user_id])
user_request_1.update_attribute(:visits, user_request_1.visits+1)
user_request_2.update_attribute(:visits, user_request_2.visits+1)
&lt;/code&gt;&lt;/pre&gt;

The problem here is that visits should really be 3, not 2.

With web applications you need multiple application servers to be able to execute the code concurrently. The database is the perfect fit to handle this case.

Changing the code to:
&lt;pre&gt;&lt;code class="ruby"&gt;
User.transaction do
  user = User.find(session[:user_id], :lock =&#62; true)
  user.update_attribute(:visits, user.visits+1)
end
&lt;/code&gt;&lt;/pre&gt;

produces the following SQL code:

&lt;pre&gt;&lt;code class="sql"&gt;
START TRANSACTION;
SELECT * FROM users WHERE id = 1 FOR UPDATE;
UPDATE users SET visits = 2, (...) WHERE id = 1;
COMMIT;
&lt;/code&gt;&lt;/pre&gt;

Does that make more sense?

(updated to remove inaccurate SQL comments)</description>
		<content:encoded><![CDATA[<p>Sorry SRTech, a full explanation would have been much more helpful.</p>
<p>Read locks such as this ensure that no other queries can read that table row until the transaction has completed. Because you want to be able to execute your code simultaneously, and from different servers, database locks can be used to make sure your is consistent.</p>
<p>For example, say you want to implement something that tracks the visits for your user accounts.</p>
<p>A simple implementation could be:</p>
<pre><code class="ruby">
user = User.find(session[:user_id])
user.update_attribute(:visits, user.visits+1)
</code></pre>
<p>If the user makes simultaneous requests you can get what&#8217;s called a <a href="http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/avoid-race.html" rel="nofollow">race condition</a>.</p>
<p>For example, the following could be the order of execution if the the same user made 2 simultaneous requests:</p>
<pre><code class="ruby">
user_request_1 = User.find(session[:user_id])
user_request_2 = User.find(session[:user_id])
user_request_1.update_attribute(:visits, user_request_1.visits+1)
user_request_2.update_attribute(:visits, user_request_2.visits+1)
</code></pre>
<p>The problem here is that visits should really be 3, not 2.</p>
<p>With web applications you need multiple application servers to be able to execute the code concurrently. The database is the perfect fit to handle this case.</p>
<p>Changing the code to:</p>
<pre><code class="ruby">
User.transaction do
  user = User.find(session[:user_id], :lock =&gt; true)
  user.update_attribute(:visits, user.visits+1)
end
</code></pre>
<p>produces the following SQL code:</p>
<pre><code class="sql">
START TRANSACTION;
SELECT * FROM users WHERE id = 1 FOR UPDATE;
UPDATE users SET visits = 2, (...) WHERE id = 1;
COMMIT;
</code></pre>
<p>Does that make more sense?</p>
<p>(updated to remove inaccurate SQL comments)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: SRTech</title>
		<link>http://www.sitepoint.com/blogs/2006/06/21/activerecord-receives-pessimistic-locking/#comment-31731</link>
		<dc:creator>SRTech</dc:creator>
		<pubDate>Wed, 21 Jun 2006 16:08:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1588#comment-31731</guid>
		<description>Tim,

I am having trouble figuring out what this does. Can you explain for those of us new to this, and give us an example of what we could use it for?</description>
		<content:encoded><![CDATA[<p>Tim,</p>
<p>I am having trouble figuring out what this does. Can you explain for those of us new to this, and give us an example of what we could use it for?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: timlucas</title>
		<link>http://www.sitepoint.com/blogs/2006/06/21/activerecord-receives-pessimistic-locking/#comment-31566</link>
		<dc:creator>timlucas</dc:creator>
		<pubDate>Wed, 21 Jun 2006 01:02:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1588#comment-31566</guid>
		<description>I know I sorely missed this feature. Not sure about others... but thanks for the input Tex ;)</description>
		<content:encoded><![CDATA[<p>I know I sorely missed this feature. Not sure about others&#8230; but thanks for the input Tex ;)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Tex</title>
		<link>http://www.sitepoint.com/blogs/2006/06/21/activerecord-receives-pessimistic-locking/#comment-31548</link>
		<dc:creator>Tex</dc:creator>
		<pubDate>Tue, 20 Jun 2006 23:24:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1588#comment-31548</guid>
		<description>Wow ground breaking.</description>
		<content:encoded><![CDATA[<p>Wow ground breaking.</p>]]></content:encoded>
	</item>
</channel>
</rss>
