<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Module: ActiveRecord::Locking::Pessimistic</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Module</strong></td> <td class="class-name-in-header">ActiveRecord::Locking::Pessimistic</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../../files/lib/active_record/locking/pessimistic_rb.html"> lib/active_record/locking/pessimistic.rb </a> <br /> </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> <a href="Pessimistic.html">Locking::Pessimistic</a> provides support for row-level locking using SELECT … FOR UPDATE and other lock types. </p> <p> Pass <tt>:lock => true</tt> to <a href="../Base.html#M000458">ActiveRecord::Base.find</a> to obtain an exclusive lock on the selected rows: </p> <pre> # select * from accounts where id=1 for update Account.find(1, :lock => true) </pre> <p> Pass <tt>:lock => ‘some locking clause‘</tt> to give a database-specific locking clause of your own such as ‘LOCK IN SHARE MODE’ or ‘FOR UPDATE NOWAIT’. </p> <p> Example: </p> <pre> Account.transaction do # select * from accounts where name = 'shugo' limit 1 for update shugo = Account.find(:first, :conditions => "name = 'shugo'", :lock => true) yuko = Account.find(:first, :conditions => "name = 'yuko'", :lock => true) shugo.balance -= 100 shugo.save! yuko.balance += 100 yuko.save! end </pre> <p> You can also use ActiveRecord::Base#lock! method to lock one record by id. This may be better if you don‘t need to lock every row. Example: </p> <pre> Account.transaction do # select * from accounts where ... accounts = Account.find(:all, :conditions => ...) account1 = accounts.detect { |account| ... } account2 = accounts.detect { |account| ... } # select * from accounts where id=? for update account1.lock! account2.lock! account1.balance -= 100 account1.save! account2.balance += 100 account2.save! end </pre> <p> Database-specific information on row locking: </p> <pre> MySQL: http://dev.mysql.com/doc/refman/5.1/en/innodb-locking-reads.html PostgreSQL: http://www.postgresql.org/docs/8.1/interactive/sql-select.html#SQL-FOR-UPDATE-SHARE </pre> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000021">lock!</a> </div> </div> </div> <!-- if includes --> <div id="section"> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000021" class="method-detail"> <a name="M000021"></a> <div class="method-heading"> <a href="Pessimistic.src/M000021.html" target="Code" class="method-signature" onclick="popupCode('Pessimistic.src/M000021.html');return false;"> <span class="method-name">lock!</span><span class="method-args">(lock = true)</span> </a> </div> <div class="method-description"> <p> Obtain a row lock on this record. Reloads the record to obtain the requested lock. Pass an SQL locking clause to append the end of the SELECT statement or pass true for "FOR UPDATE" (the default, an exclusive row lock). Returns the locked record. </p> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>