Commit 7aacde84 authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Made transaction.managed a no-op and deprecated it.

enter_transaction_management() was nearly always followed by managed().

In three places it wasn't, but they will all be refactored eventually.
The "forced" keyword argument avoids introducing behavior changes until
then.

This is mostly backwards-compatible, except, of course, for managed
itself. There's a minor difference in _enter_transaction_management:
the top self.transaction_state now contains the new 'managed' state
rather than the previous one. Django doesn't access
self.transaction_state in _enter_transaction_management.
parent 9cec689e
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -75,7 +75,6 @@ class Command(BaseCommand):
        if commit:
            transaction.commit_unless_managed(using=self.using)
            transaction.enter_transaction_management(using=self.using)
            transaction.managed(True, using=self.using)

        class SingleZipReader(zipfile.ZipFile):
            def __init__(self, *args, **kwargs):
+7 −22
Original line number Diff line number Diff line
@@ -234,7 +234,7 @@ class BaseDatabaseWrapper(object):

    ##### Generic transaction management methods #####

    def enter_transaction_management(self, managed=True):
    def enter_transaction_management(self, managed=True, forced=False):
        """
        Enters transaction management for a running thread. It must be balanced with
        the appropriate leave_transaction_management call, since the actual state is
@@ -243,12 +243,14 @@ class BaseDatabaseWrapper(object):
        The state and dirty flag are carried over from the surrounding block or
        from the settings, if there is no surrounding block (dirty is always false
        when no current block is running).

        If you switch off transaction management and there is a pending
        commit/rollback, the data will be commited, unless "forced" is True.
        """
        if self.transaction_state:
            self.transaction_state.append(self.transaction_state[-1])
        else:
            self.transaction_state.append(settings.TRANSACTIONS_MANAGED)
        self.transaction_state.append(managed)
        self._enter_transaction_management(managed)
        if not managed and self.is_dirty() and not forced:
            self.commit()

    def leave_transaction_management(self):
        """
@@ -314,22 +316,6 @@ class BaseDatabaseWrapper(object):
            return self.transaction_state[-1]
        return settings.TRANSACTIONS_MANAGED

    def managed(self, flag=True):
        """
        Puts the transaction manager into a manual state: managed transactions have
        to be committed explicitly by the user. If you switch off transaction
        management and there is a pending commit/rollback, the data will be
        commited.
        """
        top = self.transaction_state
        if top:
            top[-1] = flag
            if not flag and self.is_dirty():
                self.commit()
        else:
            raise TransactionManagementError("This code isn't under transaction "
                "management")

    def commit_unless_managed(self):
        """
        Commits changes if the system is not in managed transaction mode.
@@ -574,7 +560,6 @@ class BaseDatabaseFeatures(object):
            # otherwise autocommit will cause the confimation to
            # fail.
            self.connection.enter_transaction_management()
            self.connection.managed(True)
            cursor = self.connection.cursor()
            cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
            self.connection.commit()
+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ def force_managed(func):
    @wraps(func)
    def decorated(self, *args, **kwargs):
        if not transaction.is_managed(using=self.using):
            transaction.enter_transaction_management(using=self.using)
            transaction.enter_transaction_management(using=self.using, forced=True)
            forced_managed = True
        else:
            forced_managed = False
+2 −2
Original line number Diff line number Diff line
@@ -443,7 +443,7 @@ class QuerySet(object):
        connection = connections[self.db]
        fields = self.model._meta.local_fields
        if not transaction.is_managed(using=self.db):
            transaction.enter_transaction_management(using=self.db)
            transaction.enter_transaction_management(using=self.db, forced=True)
            forced_managed = True
        else:
            forced_managed = False
@@ -582,7 +582,7 @@ class QuerySet(object):
        query = self.query.clone(sql.UpdateQuery)
        query.add_update_values(kwargs)
        if not transaction.is_managed(using=self.db):
            transaction.enter_transaction_management(using=self.db)
            transaction.enter_transaction_management(using=self.db, forced=True)
            forced_managed = True
        else:
            forced_managed = False
+6 −12
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@ Managed transactions don't do those commits, but will need some kind of manual
or implicit commits or rollbacks.
"""

import warnings

from functools import wraps

from django.db import connections, DEFAULT_DB_ALIAS
@@ -49,7 +51,7 @@ def abort(using=None):
    """
    get_connection(using).abort()

def enter_transaction_management(managed=True, using=None):
def enter_transaction_management(managed=True, using=None, forced=False):
    """
    Enters transaction management for a running thread. It must be balanced with
    the appropriate leave_transaction_management call, since the actual state is
@@ -59,7 +61,7 @@ def enter_transaction_management(managed=True, using=None):
    from the settings, if there is no surrounding block (dirty is always false
    when no current block is running).
    """
    get_connection(using).enter_transaction_management(managed)
    get_connection(using).enter_transaction_management(managed, forced)

def leave_transaction_management(using=None):
    """
@@ -105,13 +107,8 @@ def is_managed(using=None):
    return get_connection(using).is_managed()

def managed(flag=True, using=None):
    """
    Puts the transaction manager into a manual state: managed transactions have
    to be committed explicitly by the user. If you switch off transaction
    management and there is a pending commit/rollback, the data will be
    commited.
    """
    get_connection(using).managed(flag)
    warnings.warn("'managed' no longer serves a purpose.",
        PendingDeprecationWarning, stacklevel=2)

def commit_unless_managed(using=None):
    """
@@ -224,7 +221,6 @@ def autocommit(using=None):
    """
    def entering(using):
        enter_transaction_management(managed=False, using=using)
        managed(False, using=using)

    def exiting(exc_value, using):
        leave_transaction_management(using=using)
@@ -240,7 +236,6 @@ def commit_on_success(using=None):
    """
    def entering(using):
        enter_transaction_management(using=using)
        managed(True, using=using)

    def exiting(exc_value, using):
        try:
@@ -268,7 +263,6 @@ def commit_manually(using=None):
    """
    def entering(using):
        enter_transaction_management(using=using)
        managed(True, using=using)

    def exiting(exc_value, using):
        leave_transaction_management(using=using)
Loading