Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 57efe471f3561e70a829edf1b0e9f507 > files > 2374

python-django-1.1.4-0.1mdv2010.2.noarch.rpm

<!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">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Managing database transactions &mdash; Django v1.1 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django v1.1 documentation" href="../../index.html" />
    <link rel="up" title="Models and databases" href="index.html" />
    <link rel="next" title="Database access optimization" href="optimization.html" />
    <link rel="prev" title="Performing raw SQL queries" href="sql.html" />
 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>

  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django v1.1 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="sql.html" title="Performing raw SQL queries">previous</a> 
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="optimization.html" title="Database access optimization">next</a> &raquo;</div>
    </div>
    
    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-db-transactions">
            
  <div class="section" id="s-managing-database-transactions">
<span id="s-topics-db-transactions"></span><span id="managing-database-transactions"></span><span id="topics-db-transactions"></span><h1>Managing database transactions<a class="headerlink" href="#managing-database-transactions" title="Permalink to this headline">¶</a></h1>
<p>Django gives you a few ways to control how database transactions are managed,
if you&#8217;re using a database that supports transactions.</p>
<div class="section" id="s-django-s-default-transaction-behavior">
<span id="django-s-default-transaction-behavior"></span><h2>Django&#8217;s default transaction behavior<a class="headerlink" href="#django-s-default-transaction-behavior" title="Permalink to this headline">¶</a></h2>
<p>Django&#8217;s default behavior is to run with an open transaction which it
commits automatically when any built-in, data-altering model function is
called. For example, if you call <tt class="docutils literal"><span class="pre">model.save()</span></tt> or <tt class="docutils literal"><span class="pre">model.delete()</span></tt>, the
change will be committed immediately.</p>
<p>This is much like the auto-commit setting for most databases. As soon as you
perform an action that needs to write to the database, Django produces the
<tt class="docutils literal"><span class="pre">INSERT</span></tt>/<tt class="docutils literal"><span class="pre">UPDATE</span></tt>/<tt class="docutils literal"><span class="pre">DELETE</span></tt> statements and then does the <tt class="docutils literal"><span class="pre">COMMIT</span></tt>.
There&#8217;s no implicit <tt class="docutils literal"><span class="pre">ROLLBACK</span></tt>.</p>
</div>
<div class="section" id="s-tying-transactions-to-http-requests">
<span id="tying-transactions-to-http-requests"></span><h2>Tying transactions to HTTP requests<a class="headerlink" href="#tying-transactions-to-http-requests" title="Permalink to this headline">¶</a></h2>
<p>The recommended way to handle transactions in Web requests is to tie them to
the request and response phases via Django&#8217;s <tt class="docutils literal"><span class="pre">TransactionMiddleware</span></tt>.</p>
<p>It works like this: When a request starts, Django starts a transaction. If the
response is produced without problems, Django commits any pending transactions.
If the view function produces an exception, Django rolls back any pending
transactions.</p>
<p>To activate this feature, just add the <tt class="docutils literal"><span class="pre">TransactionMiddleware</span></tt> middleware to
your <tt class="docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></tt> setting:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">MIDDLEWARE_CLASSES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="s">&#39;django.contrib.sessions.middleware.SessionMiddleware&#39;</span><span class="p">,</span>
    <span class="s">&#39;django.middleware.common.CommonMiddleware&#39;</span><span class="p">,</span>
    <span class="s">&#39;django.middleware.cache.CacheMiddleware&#39;</span><span class="p">,</span>
    <span class="s">&#39;django.middleware.transaction.TransactionMiddleware&#39;</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The order is quite important. The transaction middleware applies not only to
view functions, but also for all middleware modules that come after it. So if
you use the session middleware after the transaction middleware, session
creation will be part of the transaction.</p>
<p>An exception is <tt class="docutils literal"><span class="pre">CacheMiddleware</span></tt>, which is never affected. The cache
middleware uses its own database cursor (which is mapped to its own database
connection internally).</p>
</div>
<div class="section" id="s-controlling-transaction-management-in-views">
<span id="controlling-transaction-management-in-views"></span><h2>Controlling transaction management in views<a class="headerlink" href="#controlling-transaction-management-in-views" title="Permalink to this headline">¶</a></h2>
<p>For most people, implicit request-based transactions work wonderfully. However,
if you need more fine-grained control over how transactions are managed, you
can use Python decorators to change the way transactions are handled by a
particular view function.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Although the examples below use view functions as examples, these
decorators can be applied to non-view functions as well.</p>
</div>
<div class="section" id="s-django-db-transaction-autocommit">
<span id="s-topics-db-transactions-autocommit"></span><span id="django-db-transaction-autocommit"></span><span id="topics-db-transactions-autocommit"></span><h3><tt class="docutils literal"><span class="pre">django.db.transaction.autocommit</span></tt><a class="headerlink" href="#django-db-transaction-autocommit" title="Permalink to this headline">¶</a></h3>
<p>Use the <tt class="docutils literal"><span class="pre">autocommit</span></tt> decorator to switch a view function to Django's default
commit behavior, regardless of the global transaction setting.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">transaction</span>

<span class="nd">@transaction.autocommit</span>
<span class="k">def</span> <span class="nf">viewfunc</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="o">....</span>
</pre></div>
</div>
<p>Within <tt class="docutils literal"><span class="pre">viewfunc()</span></tt>, transactions will be committed as soon as you call
<tt class="docutils literal"><span class="pre">model.save()</span></tt>, <tt class="docutils literal"><span class="pre">model.delete()</span></tt>, or any other function that writes to the
database.</p>
</div>
<div class="section" id="s-django-db-transaction-commit-on-success">
<span id="django-db-transaction-commit-on-success"></span><h3><tt class="docutils literal"><span class="pre">django.db.transaction.commit_on_success</span></tt><a class="headerlink" href="#django-db-transaction-commit-on-success" title="Permalink to this headline">¶</a></h3>
<p>Use the <tt class="docutils literal"><span class="pre">commit_on_success</span></tt> decorator to use a single transaction for
all the work done in a function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">transaction</span>

<span class="nd">@transaction.commit_on_success</span>
<span class="k">def</span> <span class="nf">viewfunc</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="o">....</span>
</pre></div>
</div>
<p>If the function returns successfully, then Django will commit all work done
within the function at that point. If the function raises an exception, though,
Django will roll back the transaction.</p>
</div>
<div class="section" id="s-django-db-transaction-commit-manually">
<span id="django-db-transaction-commit-manually"></span><h3><tt class="docutils literal"><span class="pre">django.db.transaction.commit_manually</span></tt><a class="headerlink" href="#django-db-transaction-commit-manually" title="Permalink to this headline">¶</a></h3>
<p>Use the <tt class="docutils literal"><span class="pre">commit_manually</span></tt> decorator if you need full control over
transactions. It tells Django you'll be managing the transaction on your own.</p>
<p>If your view changes data and doesn't <tt class="docutils literal"><span class="pre">commit()</span></tt> or <tt class="docutils literal"><span class="pre">rollback()</span></tt>, Django
will raise a <tt class="docutils literal"><span class="pre">TransactionManagementError</span></tt> exception.</p>
<p>Manual transaction management looks like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">transaction</span>

<span class="nd">@transaction.commit_manually</span>
<span class="k">def</span> <span class="nf">viewfunc</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="c"># You can commit/rollback however and whenever you want</span>
    <span class="n">transaction</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
    <span class="o">...</span>

    <span class="c"># But you&#39;ve got to remember to do it yourself!</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="o">...</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="n">transaction</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">transaction</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
</pre></div>
</div>
<div class="admonition-an-important-note-to-users-of-earlier-django-releases admonition ">
<p class="first admonition-title">An important note to users of earlier Django releases:</p>
<p class="last">The database <tt class="docutils literal"><span class="pre">connection.commit()</span></tt> and <tt class="docutils literal"><span class="pre">connection.rollback()</span></tt> methods
(called <tt class="docutils literal"><span class="pre">db.commit()</span></tt> and <tt class="docutils literal"><span class="pre">db.rollback()</span></tt> in 0.91 and earlier) no
longer exist. They've been replaced by <tt class="docutils literal"><span class="pre">transaction.commit()</span></tt> and
<tt class="docutils literal"><span class="pre">transaction.rollback()</span></tt>.</p>
</div>
</div>
</div>
<div class="section" id="s-how-to-globally-deactivate-transaction-management">
<span id="how-to-globally-deactivate-transaction-management"></span><h2>How to globally deactivate transaction management<a class="headerlink" href="#how-to-globally-deactivate-transaction-management" title="Permalink to this headline">¶</a></h2>
<p>Control freaks can totally disable all transaction management by setting
<tt class="docutils literal"><span class="pre">DISABLE_TRANSACTION_MANAGEMENT</span></tt> to <tt class="xref docutils literal"><span class="pre">True</span></tt> in the Django settings file.</p>
<p>If you do this, Django won't provide any automatic transaction management
whatsoever. Middleware will no longer implicitly commit transactions, and
you'll need to roll management yourself. This even requires you to commit
changes done by middleware somewhere else.</p>
<p>Thus, this is best used in situations where you want to run your own
transaction-controlling middleware or do something really strange. In almost
all situations, you'll be better off using the default behavior, or the
transaction middleware, and only modify selected functions as needed.</p>
</div>
<div class="section" id="s-savepoints">
<span id="s-topics-db-transactions-savepoints"></span><span id="savepoints"></span><span id="topics-db-transactions-savepoints"></span><h2>Savepoints<a class="headerlink" href="#savepoints" title="Permalink to this headline">¶</a></h2>
<p>A savepoint is a marker within a transaction that enables you to roll back
part of a transaction, rather than the full transaction. Savepoints are
available to the PostgreSQL 8 and Oracle backends. Other backends will
provide the savepoint functions, but they are empty operations - they won't
actually do anything.</p>
<p>Savepoints aren't especially useful if you are using the default
<tt class="docutils literal"><span class="pre">autocommit</span></tt> behaviour of Django. However, if you are using
<tt class="docutils literal"><span class="pre">commit_on_success</span></tt> or <tt class="docutils literal"><span class="pre">commit_manually</span></tt>, each open transaction will build
up a series of database operations, awaiting a commit or rollback. If you
issue a rollback, the entire transaction is rolled back. Savepoints provide
the ability to perform a fine-grained rollback, rather than the full rollback
that would be performed by <tt class="docutils literal"><span class="pre">transaction.rollback()</span></tt>.</p>
<p>Savepoints are controlled by three methods on the transaction object:</p>
<dl class="method">
<dt id="django.db.transaction.savepoint">
<tt class="descclassname">transaction.</tt><tt class="descname">savepoint</tt>()<a class="headerlink" href="#django.db.transaction.savepoint" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a new savepoint. This marks a point in the transaction that
is known to be in a &quot;good&quot; state.</p>
<p>Returns the savepoint ID (sid).</p>
</dd></dl>

<dl class="method">
<dt id="django.db.transaction.savepoint_commit">
<tt class="descclassname">transaction.</tt><tt class="descname">savepoint_commit</tt>(<em>sid</em>)<a class="headerlink" href="#django.db.transaction.savepoint_commit" title="Permalink to this definition">¶</a></dt>
<dd>Updates the savepoint to include any operations that have been performed
since the savepoint was created, or since the last commit.</dd></dl>

<dl class="method">
<dt id="django.db.transaction.savepoint_rollback">
<tt class="descclassname">transaction.</tt><tt class="descname">savepoint_rollback</tt>(<em>sid</em>)<a class="headerlink" href="#django.db.transaction.savepoint_rollback" title="Permalink to this definition">¶</a></dt>
<dd>Rolls the transaction back to the last point at which the savepoint was
committed.</dd></dl>

<p>The following example demonstrates the use of savepoints:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">transaction</span>

<span class="nd">@transaction.commit_manually</span>
<span class="k">def</span> <span class="nf">viewfunc</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>

  <span class="n">a</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
  <span class="c"># open transaction now contains a.save()</span>
  <span class="n">sid</span> <span class="o">=</span> <span class="n">transaction</span><span class="o">.</span><span class="n">savepoint</span><span class="p">()</span>

  <span class="n">b</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
  <span class="c"># open transaction now contains a.save() and b.save()</span>

  <span class="k">if</span> <span class="n">want_to_keep_b</span><span class="p">:</span>
      <span class="n">transaction</span><span class="o">.</span><span class="n">savepoint_commit</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>
      <span class="c"># open transaction still contains a.save() and b.save()</span>
  <span class="k">else</span><span class="p">:</span>
      <span class="n">transaction</span><span class="o">.</span><span class="n">savepoint_rollback</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>
      <span class="c"># open transaction now contains only a.save()</span>

  <span class="n">transaction</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="s-transactions-in-mysql">
<span id="transactions-in-mysql"></span><h2>Transactions in MySQL<a class="headerlink" href="#transactions-in-mysql" title="Permalink to this headline">¶</a></h2>
<p>If you're using MySQL, your tables may or may not support transactions; it
depends on your MySQL version and the table types you're using. (By
&quot;table types,&quot; we mean something like &quot;InnoDB&quot; or &quot;MyISAM&quot;.) MySQL transaction
peculiarities are outside the scope of this article, but the MySQL site has
<a class="reference external" href="http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-transactions.html">information on MySQL transactions</a>.</p>
<p>If your MySQL setup does <em>not</em> support transactions, then Django will function
in auto-commit mode: Statements will be executed and committed as soon as
they're called. If your MySQL setup <em>does</em> support transactions, Django will
handle transactions as explained in this document.</p>
</div>
<div class="section" id="s-handling-exceptions-within-postgresql-transactions">
<span id="handling-exceptions-within-postgresql-transactions"></span><h2>Handling exceptions within PostgreSQL transactions<a class="headerlink" href="#handling-exceptions-within-postgresql-transactions" title="Permalink to this headline">¶</a></h2>
<p>When a call to a PostgreSQL cursor raises an exception (typically
<tt class="docutils literal"><span class="pre">IntegrityError</span></tt>), all subsequent SQL in the same transaction will fail with
the error &quot;current transaction is aborted, queries ignored until end of
transaction block&quot;. Whilst simple use of <tt class="docutils literal"><span class="pre">save()</span></tt> is unlikely to raise an
exception in PostgreSQL, there are more advanced usage patterns which
might, such as saving objects with unique fields, saving using the
force_insert/force_update flag, or invoking custom SQL.</p>
<p>There are several ways to recover from this sort of error.</p>
<div class="section" id="s-transaction-rollback">
<span id="transaction-rollback"></span><h3>Transaction rollback<a class="headerlink" href="#transaction-rollback" title="Permalink to this headline">¶</a></h3>
<p>The first option is to roll back the entire transaction. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">a</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="c"># Succeeds, but may be undone by transaction rollback</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">b</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="c"># Could throw exception</span>
<span class="k">except</span> <span class="n">IntegrityError</span><span class="p">:</span>
    <span class="n">transaction</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="c"># Succeeds, but a.save() may have been undone</span>
</pre></div>
</div>
<p>Calling <tt class="docutils literal"><span class="pre">transaction.rollback()</span></tt> rolls back the entire transaction. Any
uncommitted database operations will be lost. In this example, the changes
made by <tt class="docutils literal"><span class="pre">a.save()</span></tt> would be lost, even though that operation raised no error
itself.</p>
</div>
<div class="section" id="s-savepoint-rollback">
<span id="savepoint-rollback"></span><h3>Savepoint rollback<a class="headerlink" href="#savepoint-rollback" title="Permalink to this headline">¶</a></h3>
<p>If you are using PostgreSQL 8 or later, you can use <a class="reference internal" href="#topics-db-transactions-savepoints"><em>savepoints</em></a> to control the extent of a rollback.
Before performing a database operation that could fail, you can set or update
the savepoint; that way, if the operation fails, you can roll back the single
offending operation, rather than the entire transaction. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">a</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="c"># Succeeds, and never undone by savepoint rollback</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">sid</span> <span class="o">=</span> <span class="n">transaction</span><span class="o">.</span><span class="n">savepoint</span><span class="p">()</span>
    <span class="n">b</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="c"># Could throw exception</span>
    <span class="n">transaction</span><span class="o">.</span><span class="n">savepoint_commit</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>
<span class="k">except</span> <span class="n">IntegrityError</span><span class="p">:</span>
    <span class="n">transaction</span><span class="o">.</span><span class="n">savepoint_rollback</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="c"># Succeeds, and a.save() is never undone</span>
</pre></div>
</div>
<p>In this example, <tt class="docutils literal"><span class="pre">a.save()</span></tt> will not be undone in the case where
<tt class="docutils literal"><span class="pre">b.save()</span></tt> raises an exception.</p>
</div>
<div class="section" id="s-database-level-autocommit">
<span id="database-level-autocommit"></span><h3>Database-level autocommit<a class="headerlink" href="#database-level-autocommit" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.1:</span> <a class="reference external" href="../../releases/1.1.html#releases-1-1"><em>Please, see the release notes</em></a></div>
<p>With PostgreSQL 8.2 or later, there is an advanced option to run PostgreSQL
with <a class="reference external" href="../../ref/databases.html#ref-databases"><em>database-level autocommit</em></a>. If you use this option,
there is no constantly open transaction, so it is always possible to continue
after catching an exception. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">a</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="c"># succeeds</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">b</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="c"># Could throw exception</span>
<span class="k">except</span> <span class="n">IntegrityError</span><span class="p">:</span>
    <span class="k">pass</span>
<span class="n">c</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="c"># succeeds</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This is not the same as the <a class="reference internal" href="#topics-db-transactions-autocommit"><em>autocommit decorator</em></a>. When using database level autocommit
there is no database transaction at all. The <tt class="docutils literal"><span class="pre">autocommit</span></tt> decorator
still uses transactions, automatically committing each transaction when
a database modifying operation occurs.</p>
</div>
</div>
</div>
</div>


          </div>         
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="#">Managing database transactions</a><ul>
<li><a class="reference external" href="#django-s-default-transaction-behavior">Django&#8217;s default transaction behavior</a></li>
<li><a class="reference external" href="#tying-transactions-to-http-requests">Tying transactions to HTTP requests</a></li>
<li><a class="reference external" href="#controlling-transaction-management-in-views">Controlling transaction management in views</a><ul>
<li><a class="reference external" href="#django-db-transaction-autocommit"><tt class="docutils literal"><span class="pre">django.db.transaction.autocommit</span></tt></a></li>
<li><a class="reference external" href="#django-db-transaction-commit-on-success"><tt class="docutils literal"><span class="pre">django.db.transaction.commit_on_success</span></tt></a></li>
<li><a class="reference external" href="#django-db-transaction-commit-manually"><tt class="docutils literal"><span class="pre">django.db.transaction.commit_manually</span></tt></a></li>
</ul>
</li>
<li><a class="reference external" href="#how-to-globally-deactivate-transaction-management">How to globally deactivate transaction management</a></li>
<li><a class="reference external" href="#savepoints">Savepoints</a></li>
<li><a class="reference external" href="#transactions-in-mysql">Transactions in MySQL</a></li>
<li><a class="reference external" href="#handling-exceptions-within-postgresql-transactions">Handling exceptions within PostgreSQL transactions</a><ul>
<li><a class="reference external" href="#transaction-rollback">Transaction rollback</a></li>
<li><a class="reference external" href="#savepoint-rollback">Savepoint rollback</a></li>
<li><a class="reference external" href="#database-level-autocommit">Database-level autocommit</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="sql.html">Performing raw SQL queries</a></li>
    
    
      <li>Next: <a href="optimization.html">Database access optimization</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django v1.1 documentation</a>
        
          <ul><li><a href="../index.html">Using Django</a>
        
          <ul><li><a href="index.html">Models and databases</a>
        
        <ul><li>Managing database transactions</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>  

            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../../_sources/topics/db/transactions.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Feb 18, 2011</p>
          </div> 
        
      
    </div>
    
    <div id="ft">
      <div class="nav">
    &laquo; <a href="sql.html" title="Performing raw SQL queries">previous</a> 
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="optimization.html" title="Database access optimization">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>