Commit ac37ed21 authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Deprecated TransactionMiddleware and TRANSACTIONS_MANAGED.

Replaced them with per-database options, for proper multi-db support.

Also toned down the recommendation to tie transactions to HTTP requests.
Thanks Jeremy for sharing his experience.
parent f7245b83
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -6,10 +6,10 @@ import types

from django import http
from django.conf import settings
from django.core import exceptions
from django.core import urlresolvers
from django.core import signals
from django.core.exceptions import MiddlewareNotUsed, PermissionDenied
from django.db import connections, transaction
from django.utils.encoding import force_text
from django.utils.module_loading import import_by_path
from django.utils import six
@@ -65,6 +65,13 @@ class BaseHandler(object):
        # as a flag for initialization being complete.
        self._request_middleware = request_middleware

    def make_view_atomic(self, view):
        if getattr(view, 'transactions_per_request', True):
            for db in connections.all():
                if db.settings_dict['ATOMIC_REQUESTS']:
                    view = transaction.atomic(using=db.alias)(view)
        return view

    def get_response(self, request):
        "Returns an HttpResponse object for the given HttpRequest"
        try:
@@ -101,8 +108,9 @@ class BaseHandler(object):
                            break

                if response is None:
                    wrapped_callback = self.make_view_atomic(callback)
                    try:
                        response = callback(request, *callback_args, **callback_kwargs)
                        response = wrapped_callback(request, *callback_args, **callback_kwargs)
                    except Exception as e:
                        # If the view raised an exception, run it through exception
                        # middleware, and if the exception middleware returns a
+2 −2
Original line number Diff line number Diff line
@@ -104,7 +104,7 @@ class BaseDatabaseWrapper(object):
        conn_params = self.get_connection_params()
        self.connection = self.get_new_connection(conn_params)
        self.init_connection_state()
        if not settings.TRANSACTIONS_MANAGED:
        if self.settings_dict['AUTOCOMMIT']:
            self.set_autocommit()
        connection_created.send(sender=self.__class__, connection=self)

@@ -299,7 +299,7 @@ class BaseDatabaseWrapper(object):
        if self.transaction_state:
            managed = self.transaction_state[-1]
        else:
            managed = settings.TRANSACTIONS_MANAGED
            managed = not self.settings_dict['AUTOCOMMIT']

        if self._dirty:
            self.rollback()
+8 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ from functools import wraps
import os
import pkgutil
from threading import local
import warnings

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
@@ -158,6 +159,13 @@ class ConnectionHandler(object):
        except KeyError:
            raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)

        conn.setdefault('ATOMIC_REQUESTS', False)
        if settings.TRANSACTIONS_MANAGED:
            warnings.warn(
                "TRANSACTIONS_MANAGED is deprecated. Use AUTOCOMMIT instead.",
                PendingDeprecationWarning, stacklevel=2)
            conn.setdefault('AUTOCOMMIT', False)
        conn.setdefault('AUTOCOMMIT', True)
        conn.setdefault('ENGINE', 'django.db.backends.dummy')
        if conn['ENGINE'] == 'django.db.backends.' or not conn['ENGINE']:
            conn['ENGINE'] = 'django.db.backends.dummy'
+12 −1
Original line number Diff line number Diff line
from django.db import transaction
import warnings

from django.core.exceptions import MiddlewareNotUsed
from django.db import connection, transaction

class TransactionMiddleware(object):
    """
@@ -7,6 +10,14 @@ class TransactionMiddleware(object):
    commit, the commit is done when a successful response is created. If an
    exception happens, the database is rolled back.
    """

    def __init__(self):
        warnings.warn(
            "TransactionMiddleware is deprecated in favor of ATOMIC_REQUESTS.",
            PendingDeprecationWarning, stacklevel=2)
        if connection.settings_dict['ATOMIC_REQUESTS']:
            raise MiddlewareNotUsed

    def process_request(self, request):
        """Enters transaction management"""
        transaction.enter_transaction_management()
+8 −3
Original line number Diff line number Diff line
@@ -329,9 +329,14 @@ these changes.
1.8
---

* The decorators and context managers ``django.db.transaction.autocommit``,
  ``commit_on_success`` and ``commit_manually`` will be removed. See
  :ref:`transactions-upgrading-from-1.5`.
* The following transaction management APIs will be removed:

  - ``TransactionMiddleware``,
  - the decorators and context managers ``autocommit``, ``commit_on_success``,
    and ``commit_manually``,
  - the ``TRANSACTIONS_MANAGED`` setting.

  Upgrade paths are described in :ref:`transactions-upgrading-from-1.5`.

* The :ttag:`cycle` and :ttag:`firstof` template tags will auto-escape their
  arguments. In 1.6 and 1.7, this behavior is provided by the version of these
Loading