Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > d51872cc0e1fdee944d71993f94ac764 > files > 610

ruby-activerecord-2.3.4-1mdv2010.0.noarch.rpm

<?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 &#8230; FOR UPDATE and other lock types.
</p>
<p>
Pass <tt>:lock =&gt; 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 =&gt; true)
</pre>
<p>
Pass <tt>:lock =&gt; &#8216;some locking clause&#8216;</tt> to give a
database-specific locking clause of your own such as &#8216;LOCK IN SHARE
MODE&#8217; or &#8216;FOR UPDATE NOWAIT&#8217;.
</p>
<p>
Example:
</p>
<pre>
  Account.transaction do
    # select * from accounts where name = 'shugo' limit 1 for update
    shugo = Account.find(:first, :conditions =&gt; &quot;name = 'shugo'&quot;, :lock =&gt; true)
    yuko = Account.find(:first, :conditions =&gt; &quot;name = 'yuko'&quot;, :lock =&gt; 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&#8216;t need to lock every row. Example:
</p>
<pre>
  Account.transaction do
    # select * from accounts where ...
    accounts = Account.find(:all, :conditions =&gt; ...)
    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>&nbsp;&nbsp;
      </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 &quot;FOR UPDATE&quot; (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>