예제 #1
0
def commit_on_success_unless_managed(using=None, savepoint=False, connection=None):
    """
    Transitory API to preserve backwards-compatibility while refactoring.

    Once the legacy transaction management is fully deprecated, this should
    simply be replaced by atomic. Until then, it's necessary to guarantee that
    a commit occurs on exit, which atomic doesn't do when it's nested.

    Unlike atomic, savepoint defaults to False because that's closer to the
    legacy behavior.
    """
    if not connection:
        assert not using_gevent()

    connection = connection or get_connection(using)
    if connection.get_autocommit() or connection.in_atomic_block:
        return atomic(using, savepoint, connection=connection)
    else:
        def entering(using, connection=None):
            pass

        def exiting(exc_type, using, connection):
            set_dirty(using=using, connection=connection)

        return _transaction_func(entering, exiting, using, connection=connection)
예제 #2
0
def _transaction_func(entering, exiting, using, connection=None):
    """
    Takes 3 things, an entering function (what to do to start this block of
    transaction management), an exiting function (what to do to end it, on both
    success and failure, and using which can be: None, indicating using is
    DEFAULT_DB_ALIAS, a callable, indicating that using is DEFAULT_DB_ALIAS and
    to return the function already wrapped.

    Returns either a Transaction objects, which is both a decorator and a
    context manager, or a wrapped function, if using is a callable.
    """
    # Note that although the first argument is *called* `using`, it
    # may actually be a function; @autocommit and @autocommit('foo')
    # are both allowed forms.
    # TODO:
    if not connection:
        assert not using_gevent()

    if using is None:
        using = DEFAULT_DB_ALIAS
    if callable(using):
        return Transaction(entering,
                           exiting,
                           DEFAULT_DB_ALIAS,
                           connection=connection)(using)
    return Transaction(entering, exiting, using, connection=connection)
예제 #3
0
def commit_on_success(using=None, connection=None):
    """
    This decorator activates commit on response. This way, if the view function
    runs successfully, a commit is made; if the viewfunc produces an exception,
    a rollback is made. This is one of the most common ways to do transaction
    control in Web apps.
    """
    warnings.warn("commit_on_success is deprecated in favor of atomic.",
                  RemovedInDjango18Warning,
                  stacklevel=2)
    if not connection:
        assert not using_gevent()

    def entering(using, connection=None):
        enter_transaction_management(using=using, connection=connection)

    def exiting(exc_type, using, connection=None):
        try:
            if exc_type is not None:
                if is_dirty(using=using, connection=connection):
                    rollback(using=using, connection=connection)
            else:
                if is_dirty(using=using, connection=connection):
                    try:
                        commit(using=using, connection=connection)
                    except:
                        rollback(using=using, connection=connection)
                        raise
        finally:
            leave_transaction_management(using=using, connection=connection)

    return _transaction_func(entering, exiting, using, connection=connection)
예제 #4
0
def commit_on_success_unless_managed(using=None,
                                     savepoint=False,
                                     connection=None):
    """
    Transitory API to preserve backwards-compatibility while refactoring.

    Once the legacy transaction management is fully deprecated, this should
    simply be replaced by atomic. Until then, it's necessary to guarantee that
    a commit occurs on exit, which atomic doesn't do when it's nested.

    Unlike atomic, savepoint defaults to False because that's closer to the
    legacy behavior.
    """
    if not connection:
        assert not using_gevent()

    connection = connection or get_connection(using)
    if connection.get_autocommit() or connection.in_atomic_block:
        return atomic(using, savepoint, connection=connection)
    else:

        def entering(using, connection=None):
            pass

        def exiting(exc_type, using, connection):
            set_dirty(using=using, connection=connection)

        return _transaction_func(entering,
                                 exiting,
                                 using,
                                 connection=connection)
예제 #5
0
def commit_on_success(using=None, connection=None):
    """
    This decorator activates commit on response. This way, if the view function
    runs successfully, a commit is made; if the viewfunc produces an exception,
    a rollback is made. This is one of the most common ways to do transaction
    control in Web apps.
    """
    warnings.warn("commit_on_success is deprecated in favor of atomic.", RemovedInDjango18Warning, stacklevel=2)
    if not connection:
        assert not using_gevent()

    def entering(using, connection=None):
        enter_transaction_management(using=using, connection=connection)

    def exiting(exc_type, using, connection=None):
        try:
            if exc_type is not None:
                if is_dirty(using=using, connection=connection):
                    rollback(using=using, connection=connection)
            else:
                if is_dirty(using=using, connection=connection):
                    try:
                        commit(using=using, connection=connection)
                    except:
                        rollback(using=using, connection=connection)
                        raise
        finally:
            leave_transaction_management(using=using, connection=connection)

    return _transaction_func(entering, exiting, using, connection=connection)
예제 #6
0
def get_autocommit(using=None, connection=None):
    """
    Get the autocommit status of the connection.
    """
    if connection:
        connection.get_autocommit()
    else:
        assert not using_gevent()
        return get_connection(using).get_autocommit()
예제 #7
0
def rollback(using=None, connection=None):
    """
    Rolls back a transaction and resets the dirty flag.
    """
    if connection:
        connection.rollback()
    else:
        assert not using_gevent()
        get_connection(using).rollback()
예제 #8
0
def clean_savepoints(using=None, connection=None):
    """
    Resets the counter used to generate unique savepoint ids in this thread.
    """
    if connection:
        connection.clean_savepoints()
    else:
        assert not using_gevent()
        get_connection(using).clean_savepoints()
예제 #9
0
def get_rollback(using=None, connection=None):
    """
    Gets the "needs rollback" flag -- for *advanced use* only.
    """
    if connection:
        connection.get_rollback()
    else:
        assert not using_gevent()
        return get_connection(using).get_rollback()
예제 #10
0
def clean_savepoints(using=None, connection=None):
    """
    Resets the counter used to generate unique savepoint ids in this thread.
    """
    if connection:
        connection.clean_savepoints()
    else:
        assert not using_gevent()
        get_connection(using).clean_savepoints()
예제 #11
0
def get_rollback(using=None, connection=None):
    """
    Gets the "needs rollback" flag -- for *advanced use* only.
    """
    if connection:
        connection.get_rollback()
    else:
        assert not using_gevent()
        return get_connection(using).get_rollback()
예제 #12
0
def rollback(using=None, connection=None):
    """
    Rolls back a transaction and resets the dirty flag.
    """
    if connection:
        connection.rollback()
    else:
        assert not using_gevent()
        get_connection(using).rollback()
예제 #13
0
def get_autocommit(using=None, connection=None):
    """
    Get the autocommit status of the connection.
    """
    if connection:
        connection.get_autocommit()
    else:
        assert not using_gevent()
        return get_connection(using).get_autocommit()
예제 #14
0
def get_connection(using=None):
    """
    Get a database connection by name, or the default database connection
    if no name is provided.
    """
    assert not using_gevent()

    if using is None:
        using = DEFAULT_DB_ALIAS
    return connections[using]
예제 #15
0
def savepoint_commit(sid, using=None, connection=None):
    """
    Commits the most recent savepoint (if one exists). Does nothing if
    savepoints are not supported.
    """
    if connection:
        connection.savepoint_commit(sid)
    else:
        assert not using_gevent()
        get_connection(using).savepoint_commit(sid)
예제 #16
0
def is_dirty(using=None, connection=None):
    """
    Returns True if the current transaction requires a commit for changes to
    happen.
    """
    if connection:
        connection.is_dirty()
    else:
        assert not using_gevent()
        return get_connection(using).is_dirty()
예제 #17
0
def is_dirty(using=None, connection=None):
    """
    Returns True if the current transaction requires a commit for changes to
    happen.
    """
    if connection:
        connection.is_dirty()
    else:
        assert not using_gevent()
        return get_connection(using).is_dirty()
예제 #18
0
def get_connection(using=None):
    """
    Get a database connection by name, or the default database connection
    if no name is provided.
    """
    assert not using_gevent()

    if using is None:
        using = DEFAULT_DB_ALIAS
    return connections[using]
예제 #19
0
def savepoint_commit(sid, using=None, connection=None):
    """
    Commits the most recent savepoint (if one exists). Does nothing if
    savepoints are not supported.
    """
    if connection:
        connection.savepoint_commit(sid)
    else:
        assert not using_gevent()
        get_connection(using).savepoint_commit(sid)
예제 #20
0
def leave_transaction_management(using=None, connection=None):
    """
    Leaves transaction management for a running thread. A dirty flag is carried
    over to the surrounding block, as a commit will commit all changes, even
    those from outside. (Commits are on connection level.)
    """
    if connection:
        connection.leave_transaction_management()
    else:
        assert not using_gevent()
        get_connection(using).leave_transaction_management()
예제 #21
0
def set_clean(using=None, connection=None):
    """
    Resets a dirty flag for the current thread and code streak. This can be used
    to decide in a managed block of code to decide whether a commit or rollback
    should happen.
    """
    if connection:
        connection.set_clean()
    else:
        assert not using_gevent()
        get_connection(using).set_clean()
예제 #22
0
def savepoint(using=None, connection=None):
    """
    Creates a savepoint (if supported and required by the backend) inside the
    current transaction. Returns an identifier for the savepoint that will be
    used for the subsequent rollback or commit.
    """
    if connection:
        connection.savepoint()
    else:
        assert not using_gevent()
        return get_connection(using).savepoint()
예제 #23
0
def set_clean(using=None, connection=None):
    """
    Resets a dirty flag for the current thread and code streak. This can be used
    to decide in a managed block of code to decide whether a commit or rollback
    should happen.
    """
    if connection:
        connection.set_clean()
    else:
        assert not using_gevent()
        get_connection(using).set_clean()
예제 #24
0
def set_dirty(using=None, connection=None):
    """
    Sets a dirty flag for the current thread and code streak. This can be used
    to decide in a managed block of code to decide whether there are open
    changes waiting for commit.
    """
    if connection:
        connection.set_dirty()
    else:
        assert not using_gevent()
        get_connection(using).set_dirty()
예제 #25
0
def set_dirty(using=None, connection=None):
    """
    Sets a dirty flag for the current thread and code streak. This can be used
    to decide in a managed block of code to decide whether there are open
    changes waiting for commit.
    """
    if connection:
        connection.set_dirty()
    else:
        assert not using_gevent()
        get_connection(using).set_dirty()
예제 #26
0
def leave_transaction_management(using=None, connection=None):
    """
    Leaves transaction management for a running thread. A dirty flag is carried
    over to the surrounding block, as a commit will commit all changes, even
    those from outside. (Commits are on connection level.)
    """
    if connection:
        connection.leave_transaction_management()
    else:
        assert not using_gevent()
        get_connection(using).leave_transaction_management()
예제 #27
0
def savepoint(using=None, connection=None):
    """
    Creates a savepoint (if supported and required by the backend) inside the
    current transaction. Returns an identifier for the savepoint that will be
    used for the subsequent rollback or commit.
    """
    if connection:
        connection.savepoint()
    else:
        assert not using_gevent()
        return get_connection(using).savepoint()
예제 #28
0
def atomic(using=None, savepoint=True, connection=None):
    # Bare decorator: @atomic -- although the first argument is called
    # `using`, it's actually the function being decorated.

    # @atomic
    if callable(using):
        # 在使用gevent时,connection是动态获取的,因此不能直接使用 @atomic
        assert not using_gevent()
        return Atomic(DEFAULT_DB_ALIAS, savepoint)(using)
    # Decorator: @atomic(...) or context manager: with atomic(...): ...
    else:
        # with atomic(conneciton=xxx)
        return Atomic(using, savepoint, connection=connection)
예제 #29
0
def atomic(using=None, savepoint=True, connection=None):
    # Bare decorator: @atomic -- although the first argument is called
    # `using`, it's actually the function being decorated.

    # @atomic
    if callable(using):
        # 在使用gevent时,connection是动态获取的,因此不能直接使用 @atomic
        assert not using_gevent()
        return Atomic(DEFAULT_DB_ALIAS, savepoint)(using)
    # Decorator: @atomic(...) or context manager: with atomic(...): ...
    else:
        # with atomic(conneciton=xxx)
        return Atomic(using, savepoint, connection=connection)
예제 #30
0
def commit(using=None, connection=None):
    """
    Commits a transaction and resets the dirty flag.
    """
    from django.db.backends import BaseDatabaseWrapper
    if isinstance(using, BaseDatabaseWrapper):
        connection = using
        using = None

    if connection:
        connection.commit()
    else:
        assert not using_gevent()
        get_connection(using).commit()
예제 #31
0
def commit(using=None, connection=None):
    """
    Commits a transaction and resets the dirty flag.
    """
    from django.db.backends import BaseDatabaseWrapper
    if isinstance(using, BaseDatabaseWrapper):
        connection = using
        using = None

    if connection:
        connection.commit()
    else:
        assert not using_gevent()
        get_connection(using).commit()
예제 #32
0
def enter_transaction_management(managed=True, using=None, forced=False, connection=None):
    """
    Enters transaction management for a running thread. It must be balanced with
    the appropriate leave_transaction_management call, since the actual state is
    managed as a stack.

    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 connection:
        connection.enter_transaction_management(managed, forced)
    else:
        assert not using_gevent()
        get_connection(using).enter_transaction_management(managed, forced)
예제 #33
0
def set_autocommit(autocommit, using=None, connection=None):
    """
    Set the autocommit status of the connection.
    """
    # set_autocommit(True, connection)
    from django.db.backends import BaseDatabaseWrapper
    if isinstance(using, BaseDatabaseWrapper):
        connection = using
        using = None

    if connection:
        connection.set_autocommit(autocommit)
    else:
        assert not using_gevent()
        return get_connection(using).set_autocommit(autocommit)
예제 #34
0
def set_autocommit(autocommit, using=None, connection=None):
    """
    Set the autocommit status of the connection.
    """
    # set_autocommit(True, connection)
    from django.db.backends import BaseDatabaseWrapper
    if isinstance(using, BaseDatabaseWrapper):
        connection = using
        using = None

    if connection:
        connection.set_autocommit(autocommit)
    else:
        assert not using_gevent()
        return get_connection(using).set_autocommit(autocommit)
예제 #35
0
def abort(using=None, connection=None):
    """
    Roll back any ongoing transactions and clean the transaction management
    state of the connection.

    This method is to be used only in cases where using balanced
    leave_transaction_management() calls isn't possible. For example after a
    request has finished, the transaction state isn't known, yet the connection
    must be cleaned up for the next request.
    """
    # 清空状态
    if connection:
        connection.abort()
    else:
        assert not using_gevent()
        get_connection(using).abort()
예제 #36
0
def abort(using=None, connection=None):
    """
    Roll back any ongoing transactions and clean the transaction management
    state of the connection.

    This method is to be used only in cases where using balanced
    leave_transaction_management() calls isn't possible. For example after a
    request has finished, the transaction state isn't known, yet the connection
    must be cleaned up for the next request.
    """
    # 清空状态
    if connection:
        connection.abort()
    else:
        assert not using_gevent()
        get_connection(using).abort()
예제 #37
0
def set_rollback(rollback, using=None, connection=None):
    """
    Sets or unsets the "needs rollback" flag -- for *advanced use* only.

    When `rollback` is `True`, it triggers a rollback when exiting the
    innermost enclosing atomic block that has `savepoint=True` (that's the
    default). Use this to force a rollback without raising an exception.

    When `rollback` is `False`, it prevents such a rollback. Use this only
    after rolling back to a known-good state! Otherwise, you break the atomic
    block and data corruption may occur.
    """
    if connection:
        connection.set_rollback(rollback)
    else:
        assert not using_gevent()
        return get_connection(using).set_rollback(rollback)
예제 #38
0
def set_rollback(rollback, using=None, connection=None):
    """
    Sets or unsets the "needs rollback" flag -- for *advanced use* only.

    When `rollback` is `True`, it triggers a rollback when exiting the
    innermost enclosing atomic block that has `savepoint=True` (that's the
    default). Use this to force a rollback without raising an exception.

    When `rollback` is `False`, it prevents such a rollback. Use this only
    after rolling back to a known-good state! Otherwise, you break the atomic
    block and data corruption may occur.
    """
    if connection:
        connection.set_rollback(rollback)
    else:
        assert not using_gevent()
        return get_connection(using).set_rollback(rollback)
예제 #39
0
def enter_transaction_management(managed=True,
                                 using=None,
                                 forced=False,
                                 connection=None):
    """
    Enters transaction management for a running thread. It must be balanced with
    the appropriate leave_transaction_management call, since the actual state is
    managed as a stack.

    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 connection:
        connection.enter_transaction_management(managed, forced)
    else:
        assert not using_gevent()
        get_connection(using).enter_transaction_management(managed, forced)
예제 #40
0
def autocommit(using=None, connection=None):
    """
    Decorator that activates commit on save. This is Django's default behavior;
    this decorator is useful if you globally activated transaction management in
    your settings file and want the default behavior in some view functions.
    """
    warnings.warn("autocommit is deprecated in favor of set_autocommit.",
        RemovedInDjango18Warning, stacklevel=2)

    # 如果使用gevent, 则不允许直接使用 @autocommit
    if not connection:
        assert not using_gevent()

    def entering(using, connection = None):
        enter_transaction_management(managed=False, using=using, connection=connection)

    def exiting(exc_type, using, connection = None):
        leave_transaction_management(using=using, connection=connection)

    return _transaction_func(entering, exiting, using, connection=connection)
예제 #41
0
def commit_manually(using=None, connection=None):
    """
    Decorator that activates manual transaction control. It just disables
    automatic transaction control and doesn't do any commit/rollback of its
    own -- it's up to the user to call the commit and rollback functions
    themselves.
    """
    warnings.warn("commit_manually is deprecated in favor of set_autocommit.",
        RemovedInDjango18Warning, stacklevel=2)

    if not connection:
        assert not using_gevent()

    def entering(using, connection):
        enter_transaction_management(using=using, connection=connection)

    def exiting(exc_type, using, connection):
        leave_transaction_management(using=using, connection=connection)

    return _transaction_func(entering, exiting, using, connection=connection)
예제 #42
0
def commit_manually(using=None, connection=None):
    """
    Decorator that activates manual transaction control. It just disables
    automatic transaction control and doesn't do any commit/rollback of its
    own -- it's up to the user to call the commit and rollback functions
    themselves.
    """
    warnings.warn("commit_manually is deprecated in favor of set_autocommit.",
                  RemovedInDjango18Warning,
                  stacklevel=2)

    if not connection:
        assert not using_gevent()

    def entering(using, connection):
        enter_transaction_management(using=using, connection=connection)

    def exiting(exc_type, using, connection):
        leave_transaction_management(using=using, connection=connection)

    return _transaction_func(entering, exiting, using, connection=connection)
예제 #43
0
def _transaction_func(entering, exiting, using, connection=None):
    """
    Takes 3 things, an entering function (what to do to start this block of
    transaction management), an exiting function (what to do to end it, on both
    success and failure, and using which can be: None, indicating using is
    DEFAULT_DB_ALIAS, a callable, indicating that using is DEFAULT_DB_ALIAS and
    to return the function already wrapped.

    Returns either a Transaction objects, which is both a decorator and a
    context manager, or a wrapped function, if using is a callable.
    """
    # Note that although the first argument is *called* `using`, it
    # may actually be a function; @autocommit and @autocommit('foo')
    # are both allowed forms.
    # TODO:
    if not connection:
        assert not using_gevent()

    if using is None:
        using = DEFAULT_DB_ALIAS
    if callable(using):
        return Transaction(entering, exiting, DEFAULT_DB_ALIAS, connection=connection)(using)
    return Transaction(entering, exiting, using, connection=connection)
예제 #44
0
def autocommit(using=None, connection=None):
    """
    Decorator that activates commit on save. This is Django's default behavior;
    this decorator is useful if you globally activated transaction management in
    your settings file and want the default behavior in some view functions.
    """
    warnings.warn("autocommit is deprecated in favor of set_autocommit.",
                  RemovedInDjango18Warning,
                  stacklevel=2)

    # 如果使用gevent, 则不允许直接使用 @autocommit
    if not connection:
        assert not using_gevent()

    def entering(using, connection=None):
        enter_transaction_management(managed=False,
                                     using=using,
                                     connection=connection)

    def exiting(exc_type, using, connection=None):
        leave_transaction_management(using=using, connection=connection)

    return _transaction_func(entering, exiting, using, connection=connection)
예제 #45
0
    def __init__(self, using, savepoint, connection=None):
        self.using = using
        self.savepoint = savepoint

        assert connection or (not using_gevent())
        self.connection = connection
예제 #46
0
    def __init__(self, using, savepoint, connection=None):
        self.using = using
        self.savepoint = savepoint

        assert connection or (not using_gevent())
        self.connection = connection