Django

Code

Changeset 3233

Show
Ignore:
Timestamp:
06/28/06 16:38:04 (2 years ago)
Author:
jpellerin
Message:

[multi-db] Added connections parameter to transaction
functions, where appropriate, to allow specifying which connections to
commit or rollback.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/multiple-db-support/django/db/transaction.py

    r3135 r3233  
    1717except ImportError: 
    1818    import dummy_thread as thread 
    19 from django.db import connection 
     19from django import db 
    2020from django.conf import settings 
    2121 
     
    117117    to be committed explicitly by the user. If you switch off transaction 
    118118    management and there is a pending commit/rollback, the data will be 
    119     commited. 
     119    commited. Note that managed state applies across all connections. 
    120120    """ 
    121121    thread_ident = thread.get_ident() 
     
    124124        top[-1] = flag 
    125125        if not flag and is_dirty(): 
    126             connection._commit() 
     126            for cx in all_connections(): 
     127                cx._commit() 
    127128            set_clean() 
    128129    else: 
    129130        raise TransactionManagementError("This code isn't under transaction management") 
    130131 
    131 def commit_unless_managed(): 
     132def commit_unless_managed(connections=None): 
    132133    """ 
    133134    Commits changes if the system is not in managed transaction mode. 
    134135    """ 
    135136    if not is_managed(): 
    136         connection._commit() 
     137        if connections is None: 
     138            connections = all_connections() 
     139        else: 
     140            connections = ensure_connections(connections) 
     141        for cx in connections: 
     142            cx._commit() 
    137143    else: 
    138144        set_dirty() 
    139145 
    140 def rollback_unless_managed(): 
     146def rollback_unless_managed(connections=None): 
    141147    """ 
    142148    Rolls back changes if the system is not in managed transaction mode. 
    143149    """ 
    144150    if not is_managed(): 
    145         connection._rollback() 
     151        if connections is None: 
     152            connections = all_connections() 
     153        for cx in connections: 
     154            cx._rollback() 
    146155    else: 
    147156        set_dirty() 
    148157 
    149 def commit(): 
     158def commit(connections=None): 
    150159    """ 
    151160    Does the commit itself and resets the dirty flag. 
    152161    """ 
    153     connection._commit() 
     162    if connections is None: 
     163        connections = all_connections() 
     164    else: 
     165        connections = ensure_connections(connections) 
     166    for cx in connections: 
     167        cx._commit() 
    154168    set_clean() 
    155169 
    156 def rollback(): 
     170def rollback(connections=None): 
    157171    """ 
    158172    This function does the rollback itself and resets the dirty flag. 
    159173    """ 
    160     connection._rollback() 
     174    if connections is None: 
     175        connections = all_connections() 
     176    else: 
     177        connections = ensure_connections(connections) 
     178    for cx in connections: 
     179        cx._rollback() 
    161180    set_clean() 
    162181 
     
    180199    return _autocommit 
    181200 
    182 def commit_on_success(func): 
     201def commit_on_success(func, connections=None): 
    183202    """ 
    184203    This decorator activates commit on response. This way, if the view function 
     
    199218            else: 
    200219                if is_dirty(): 
    201                     commit(
     220                    commit(connections
    202221            return res 
    203222        finally: 
     
    221240 
    222241    return _commit_manually 
     242 
     243########### 
     244# HELPERS # 
     245########### 
     246 
     247def all_connections(): 
     248    return [db.connection] + [ c.connection 
     249                               for c in db.connections.values() ] 
     250 
     251def ensure_connections(val): 
     252    connections = [] 
     253    if isinstance(val, basestring): 
     254        val = [val] 
     255    try: 
     256        iter(val) 
     257    except: 
     258        val = [val] 
     259    for cx in val: 
     260        if hasattr(cx, 'cursor'): 
     261            connections.append(cx) 
     262        elif hasattr(cx, 'connection'): 
     263            connections.append(cx.connection) 
     264        elif isinstance(cx, basestring): 
     265            connections.append(db.connections[cx].connection) 
     266    return connections 
     267