Django

Code

Changeset 2462

Show
Ignore:
Timestamp:
03/01/06 19:37:06 (3 years ago)
Author:
adrian
Message:

magic-removal: Typo corrections in transaction code and docs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/db/transaction.py

    r2457 r2462  
    1919class TransactionManagementError(Exception): 
    2020    """ 
    21     This is the exception that is thrown whe
    22     something bad happens with transaction management. 
     21    This exception is thrown when something bad happens with transactio
     22    management. 
    2323    """ 
    2424    pass 
    25      
     25 
    2626# The state is a dictionary of lists. The key to the dict is the current 
    2727# thread and the list is handled as a stack of values. 
     
    3838    the appropriate leave_transaction_management call, since the actual state is 
    3939    managed as a stack. 
    40      
    41     The state and dirty flag are carried over from the surrounding block or 
    42     from the settings, if there is no surrounding block (dirty is allways false 
     40 
     41    The state and dirty flag are carried over from the surrounding block or 
     42    from the settings, if there is no surrounding block (dirty is always false 
    4343    when no current block is running). 
    4444    """ 
     
    5656    Leaves transaction management for a running thread. A dirty flag is carried 
    5757    over to the surrounding block, as a commit will commit all changes, even 
    58     those from outside (commits are on connection level). 
     58    those from outside. (Commits are on connection level.) 
    5959    """ 
    6060    thread_ident = thread.get_ident() 
     
    6464        raise TransactionManagementError("This code isn't under transaction management") 
    6565    if dirty.get(thread_ident, False): 
    66         # I fixed it for you this time, but don't do it again! 
    6766        rollback() 
    6867        raise TransactionManagementError("Transaction managed block ended with pending COMMIT/ROLLBACK") 
     
    7170def is_dirty(): 
    7271    """ 
    73     Checks if the current transaction requires a commit for changes to happen. 
     72    Returns True if the current transaction requires a commit for changes to 
     73    happen. 
    7474    """ 
    7575    return dirty.get(thread.get_ident(), False) 
     
    9090    """ 
    9191    Resets a dirty flag for the current thread and code streak. This can be used 
    92     to decide in a managed block of code to decide whether there should happen a 
    93     commit or rollback
     92    to decide in a managed block of code to decide whether a commit or rollback 
     93    should happen
    9494    """ 
    9595    thread_ident = thread.get_ident() 
     
    111111def managed(flag=True): 
    112112    """ 
    113     Puts the transaction manager into a manual state - managed transactions have 
     113    Puts the transaction manager into a manual state: managed transactions have 
    114114    to be committed explicitely by the user. If you switch off transaction 
    115115    management and there is a pending commit/rollback, the data will be 
     
    164164def autocommit(func): 
    165165    """ 
    166     Decorator that activates commit on save. This is Django's default behavour; 
     166    Decorator that activates commit on save. This is Django's default behavior; 
    167167    this decorator is useful if you globally activated transaction management in 
    168     your settings file and want the default behaviour in some view functions. 
    169     """ 
    170  
     168    your settings file and want the default behavior in some view functions. 
     169    """ 
    171170    def _autocommit(*args, **kw): 
    172171        try: 
     
    176175        finally: 
    177176            leave_transaction_management() 
    178  
    179177    return _autocommit 
    180178 
    181179def commit_on_success(func): 
    182180    """ 
    183     This decorator activates commit on response. This way if the viewfunction 
    184     runs successfully, a commit is made, if the viewfunc produces an exception, 
     181    This decorator activates commit on response. This way, if the view function 
     182    runs successfully, a commit is made; if the viewfunc produces an exception, 
    185183    a rollback is made. This is one of the most common ways to do transaction 
    186184    control in web apps. 
    187185    """ 
    188  
    189186    def _commit_on_success(*args, **kw): 
    190187        try: 
     
    203200        finally: 
    204201            leave_transaction_management() 
    205              
    206202    return _commit_on_success 
    207203 
     
    209205    """ 
    210206    Decorator that activates manual transaction control. It just disables 
    211     automatic transaction control and doesn't do any commit/rollback of it's own 
    212     - it's up to the user to call the commit and rollback functions themselves. 
    213     """ 
    214  
     207    automatic transaction control and doesn't do any commit/rollback of its 
     208    own -- it's up to the user to call the commit and rollback functions 
     209    themselves. 
     210    """ 
    215211    def _commit_manually(*args, **kw): 
    216212        try: 
     
    222218 
    223219    return _commit_manually 
    224  
    225  
  • django/branches/magic-removal/django/middleware/transaction.py

    r2457 r2462  
    66    Transaction middleware. If this is enabled, each view function will be run 
    77    with commit_on_response activated - that way a save() doesn't do a direct 
    8     commit, the commit is done when a successfull response is created. If an 
     8    commit, the commit is done when a successful response is created. If an 
    99    exception happens, the database is rolled back. 
    1010    """ 
    11  
    1211    def process_request(self, request): 
    1312        """Enters transaction management""" 
  • django/branches/magic-removal/docs/transactions.txt

    r2458 r2462  
    99 
    1010The default behavior of Django is to commit on special model functions. If you 
    11 call ``model.save()`` or ``model.delete()``, that change will be committed immediately.  
     11call ``model.save()`` or ``model.delete()``, that change will be committed immediately. 
    1212 
    1313This is much like the auto-commit setting for most databases: as soon as you 
     
    2020 
    2121A useful way to handle transactions is to tie them to the request and response 
    22 phases.  
     22phases. 
    2323 
    2424When a request starts, you start a transaction. If the response is produced 
     
    4040session creation will be part of the transaction. 
    4141 
    42 The cache middleware isn't affected as it uses it's own database cursor (that is 
    43 mapped to it's own database connection internally) and only the database based 
    44 cache is affected. 
     42The cache middleware isn't affected, as it uses its own database cursor (which 
     43is mapped to its own database connection internally), and only the 
     44database-based cache is affected. 
    4545 
    4646Controlling transaction management in views 
    4747=========================================== 
    4848 
    49 For many people, implicit request-based transactions will work wonderfully.  
    50 However, if you need to control the way that transactions are managed,  
    51 there are a set of decorators that you can apply to a function to change 
    52 the way transactions are handled. 
     49For many people, implicit request-based transactions will work wonderfully. 
     50However, if you need to control the way that transactions are managed, 
     51you can use decorators that you can apply to a function to change the way 
     52transactions are handled. 
    5353 
    5454.. note:: 
     
    6565 
    6666    from django.db.transaction import autocommit 
    67   
     67 
    6868    @transaction.autocommit 
    6969    def viewfunc(request): 
    7070        .... 
    7171 
    72 Within ``viewfunc`` transactions will be comitted as soon as you call 
    73 ``model.save()``, ``model.delete()``, or any similar function that writes to the 
     72Within ``viewfunc`` transactions will be committed as soon as you call 
     73``model.save()``, ``model.delete()``, or any other function that writes to the 
    7474database. 
    7575 
     
    7777--------------------- 
    7878 
    79 You can use the ``commit_on_success`` decorator to use a single transaction for  
     79You can use the ``commit_on_success`` decorator to use a single transaction for 
    8080all the work done in a function:: 
    8181 
     
    8686        .... 
    8787 
    88 If the function returns successfully then all work done will be committed. If an 
     88If the function returns successfully, then all work done will be committed. If an 
    8989exception is raised beyond the function, however, the transaction will be rolled 
    9090back. 
     
    9494 
    9595Sometimes you need full control over your transactions. In that case, you can use the 
    96 ``commit_manually`` decorator which will make you run your own transaction management. 
     96``commit_manually`` decorator, which tells Django you'll be managing the transaction 
     97on your own. 
    9798 
    98 If you don't commit or rollback and did change data (so that the current transaction 
    99 is marked as dirty), you will get a ``TransactionManagementError`` exception saying so.  
     99If you don't commit or rollback and did change data (so that the current 
     100transaction is marked as dirty), you'll get a ``TransactionManagementError`` 
     101exception. 
    100102 
    101103Manual transaction management looks like:: 
     
    107109        ... 
    108110        # You can commit/rollback however and whenever you want 
    109         transaction.commit()     
     111        transaction.commit() 
    110112        ... 
    111          
     113 
    112114        # But you've got to remember to do it yourself! 
    113115        try: 
     
    118120            transaction.commit() 
    119121 
    120 ..admonition:: An important note to users of earlier django releases:  
    121      
     122..admonition:: An important note to users of earlier Django releases: 
     123 
    122124    The database ``connection.commit`` and ``connection.rollback`` functions 
    123125    (also called ``db.commit`` and ``db.rollback`` in 0.91 and earlier), no 
     
    129131 
    130132Control freaks can totally disable all transaction management by setting 
    131 ``DISABLE_TRANSACTION_MANAGEMENT`` to ``True`` in your settings file. 
     133``DISABLE_TRANSACTION_MANAGEMENT`` to ``True`` in the Django settings file. 
    132134 
    133135If you do this, there will be no management whatsoever. The middleware will no 
     
    137139 
    138140Thus, this is best used in situations where you want to run your own transaction 
    139 controlling middleware or do something really strange. In almost all situations 
     141controlling middleware or do something really strange. In almost all situations, 
    140142you'll be better off using the default behavior or the transaction middleware 
    141143and only modify selected functions as needed.