Django

Code

Changeset 2457

Show
Ignore:
Timestamp:
03/01/06 11:08:33 (3 years ago)
Author:
jacob
Message:

magic-removal: added transaction support to Django! see transactions.txt (in magic-removal) for the details.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/bin/daily_cleanup.py

    r1638 r2457  
    11"Daily cleanup file" 
    22 
    3 from django.db import backend, connection 
     3from django.db import backend, connection, transaction 
    44 
    55DOCUMENTATION_DIRECTORY = '/home/html/documentation/' 
     
    1212    cursor.execute("DELETE FROM %s WHERE %s < NOW() - INTERVAL '1 week'" % \ 
    1313        (backend.quote_name('registration_challenges'), backend.quote_name('request_date'))) 
    14     connection.commit() 
     14    transaction.commit_unless_managed() 
    1515 
    1616if __name__ == "__main__": 
  • django/branches/magic-removal/django/conf/global_settings.py

    r2295 r2457  
    195195ENABLE_PSYCO = False 
    196196 
     197# Do you want to manage transactions manually? 
     198# Hint: you really don't! 
     199TRANSACTIONS_MANAGED = False 
     200 
    197201############## 
    198202# MIDDLEWARE # 
  • django/branches/magic-removal/django/core/cache/backends/db.py

    r2388 r2457  
    22 
    33from django.core.cache.backends.base import BaseCache 
    4 from django.db import connection 
     4from django.db import connection, transaction 
    55import base64, time 
    66from datetime import datetime 
     
    3434        if row[2] < now: 
    3535            cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) 
    36             connection.commit() 
     36            transaction.commit_unless_managed() 
    3737            return default 
    3838        return pickle.loads(base64.decodestring(row[1])) 
     
    5959            pass 
    6060        else: 
    61             connection.commit() 
     61            transaction.commit_unless_managed() 
    6262 
    6363    def delete(self, key): 
    6464        cursor = connection.cursor() 
    6565        cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) 
    66         connection.commit() 
     66        transaction.commit_unless_managed() 
    6767 
    6868    def has_key(self, key): 
  • django/branches/magic-removal/django/core/management.py

    r2452 r2457  
    219219def get_sql_delete(app): 
    220220    "Returns a list of the DROP TABLE SQL statements for the given app." 
    221     from django.db import backend, connection, models 
     221    from django.db import backend, connection, models, transaction 
    222222 
    223223    try: 
     
    234234    except: 
    235235        # The table doesn't exist, so it doesn't need to be dropped. 
    236         connection.rollback() 
     236        transaction.rollback_unless_managed() 
    237237        admin_log_exists = False 
    238238    else: 
     
    253253        except: 
    254254            # The table doesn't exist, so it doesn't need to be dropped. 
    255             connection.rollback() 
     255            transaction.rollback_unless_managed() 
    256256        else: 
    257257            opts = klass._meta 
     
    269269         except: 
    270270             # The table doesn't exist, so it doesn't need to be dropped. 
    271              connection.rollback() 
     271             transaction.rollback_unless_managed() 
    272272         else: 
    273273             output.append("DROP TABLE %s;" % backend.quote_name(klass._meta.db_table)) 
     
    291291                    cursor.execute("SELECT 1 FROM %s LIMIT 1" % backend.quote_name(f.m2m_db_table())) 
    292292            except: 
    293                 connection.rollback() 
     293                transaction.rollback_unless_managed() 
    294294            else: 
    295295                output.append("DROP TABLE %s;" % backend.quote_name(f.m2m_db_table())) 
     
    404404def syncdb(): 
    405405    "Creates the database tables for all apps in INSTALLED_APPS whose tables haven't already been created." 
    406     from django.db import backend, connection, models, get_creation_module, get_introspection_module 
     406    from django.db import backend, connection, transaction, models, get_creation_module, get_introspection_module 
    407407    introspection_module = get_introspection_module() 
    408408    data_types = get_creation_module().DATA_TYPES 
     
    471471            cursor.execute(sql) 
    472472 
    473     connection.commit() 
     473    transaction.commit_unless_managed() 
    474474syncdb.args = '' 
    475475 
     
    500500def install(app): 
    501501    "Executes the equivalent of 'get_sql_all' in the current database." 
    502     from django.db import connection 
     502    from django.db import connection, transaction 
    503503    from cStringIO import StringIO 
    504504    app_name = app.__name__[app.__name__.rindex('.')+1:] 
     
    527527The full error: %s\n""" % \ 
    528528            (app_name, app_label, e)) 
    529         connection.rollback() 
     529        transaction.rollback_unless_managed() 
    530530        sys.exit(1) 
    531     connection.commit() 
     531    transaction.commit_unless_managed() 
    532532install.help_doc = "Executes ``sqlall`` for the given app(s) in the current database." 
    533533install.args = APP_ARGS 
     
    535535def reset(app): 
    536536    "Executes the equivalent of 'get_sql_reset' in the current database." 
    537     from django.db import connection 
     537    from django.db import connection, transaction 
    538538    from cStringIO import StringIO 
    539539    app_name = app.__name__[app.__name__.rindex('.')+1:] 
     
    569569The full error: %s\n""" % \ 
    570570                (app_name, app_label, e)) 
    571             connection.rollback() 
     571            transaction.rollback_unless_managed() 
    572572            sys.exit(1) 
    573         connection.commit() 
     573        transaction.commit_unless_managed() 
    574574    else: 
    575575        print "Reset cancelled." 
     
    10361036def createcachetable(tablename): 
    10371037    "Creates the table needed to use the SQL cache backend" 
    1038     from django.db import backend, get_creation_module, models 
     1038    from django.db import backend, connection, transaction, get_creation_module, models 
    10391039    data_types = get_creation_module().DATA_TYPES 
    10401040    fields = ( 
     
    10671067    for statement in index_output: 
    10681068        curs.execute(statement) 
    1069     connection.commit() 
     1069    transaction.commit_unless_managed() 
    10701070createcachetable.args = "[tablename]" 
    10711071 
  • django/branches/magic-removal/django/db/backends/ado_mssql/base.py

    r2422 r2457  
    6565        return cursor 
    6666 
    67     def commit(self): 
     67    def _commit(self): 
    6868        return self.connection.commit() 
    6969 
    70     def rollback(self): 
     70    def _rollback(self): 
    7171        if self.connection: 
    7272            return self.connection.rollback() 
  • django/branches/magic-removal/django/db/backends/dummy/base.py

    r1803 r2457  
    1818class DatabaseWrapper: 
    1919    cursor = complain 
    20     commit = complain 
    21     rollback = complain 
     20    _commit = complain 
     21    _rollback = complain 
    2222 
    2323    def close(self): 
  • django/branches/magic-removal/django/db/backends/mysql/base.py

    r2451 r2457  
    7272        return cursor 
    7373 
    74     def commit(self): 
     74    def _commit(self): 
    7575        self.connection.commit() 
    7676 
    77     def rollback(self): 
     77    def _rollback(self): 
    7878        if self.connection: 
    7979            try: 
  • django/branches/magic-removal/django/db/backends/postgresql/base.py

    r2422 r2457  
    3838        return cursor 
    3939 
    40     def commit(self): 
     40    def _commit(self): 
    4141        return self.connection.commit() 
    4242 
    43     def rollback(self): 
     43    def _rollback(self): 
    4444        if self.connection: 
    4545            return self.connection.rollback() 
  • django/branches/magic-removal/django/db/backends/sqlite3/base.py

    r2422 r2457  
    4040            return cursor 
    4141 
    42     def commit(self): 
     42    def _commit(self): 
    4343        self.connection.commit() 
    4444 
    45     def rollback(self): 
     45    def _rollback(self): 
    4646        if self.connection: 
    4747            self.connection.rollback() 
     
    6868 
    6969    def convert_query(self, query, num_params): 
    70         # XXX this seems too simple to be correct... is this right? 
    7170        return query % tuple("?" * num_params) 
    7271 
  • django/branches/magic-removal/django/db/__init__.py

    r1999 r2457  
    3939# Register an event that rolls back the connection 
    4040# when a Django request has an exception. 
    41 dispatcher.connect(lambda: connection.rollback(), signal=signals.got_request_exception) 
     41def _rollback_on_exception(): 
     42    from django.db import transaction 
     43    transaction.rollback_unless_managed() 
     44dispatcher.connect(_rollback_on_exception, signal=signals.got_request_exception) 
  • django/branches/magic-removal/django/db/models/base.py

    r2438 r2457  
    66from django.db.models.query import orderlist2sql, delete_objects 
    77from django.db.models.options import Options, AdminOptions 
    8 from django.db import connection, backend 
     8from django.db import connection, backend, transaction 
    99from django.db.models import signals 
    1010from django.db.models.loading import register_models 
     
    185185            if self._meta.has_auto_field and not pk_set: 
    186186                setattr(self, self._meta.pk.attname, backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column)) 
    187         connection.commit() 
     187        transaction.commit_unless_managed() 
    188188 
    189189        # Run any post-save hooks. 
     
    341341            backend.quote_name(rel_field.m2m_reverse_name())) 
    342342        cursor.executemany(sql, [(this_id, i) for i in id_list]) 
    343         connection.commit() 
     343        transaction.commit_unless_managed() 
    344344 
    345345############################################ 
     
    358358    rel_val = getattr(self, ordered_obj.order_with_respect_to.rel.field_name) 
    359359    cursor.executemany(sql, [(i, rel_val, j) for i, j in enumerate(id_list)]) 
    360     connection.commit() 
     360    transaction.commit_unless_managed() 
    361361 
    362362def method_get_order(ordered_obj, self): 
  • django/branches/magic-removal/django/db/models/fields/related.py

    r2437 r2457  
    1 from django.db import backend, connection 
     1from django.db import backend, connection, transaction 
    22from django.db.models import signals 
    33from django.db.models.fields import AutoField, Field, IntegerField 
     
    232232            (join_table, source_col_name, target_col_name), 
    233233            [source_pk_val, obj_id]) 
    234     connection.commit() 
     234    transaction.commit_unless_managed() 
    235235 
    236236def _remove_m2m_items(rel_model, join_table, source_col_name, 
     
    256256            (join_table, source_col_name, target_col_name), 
    257257            [source_pk_val, obj._get_pk_val()]) 
    258     connection.commit() 
     258    transaction.commit_unless_managed() 
    259259 
    260260def _clear_m2m_items(join_table, source_col_name, source_pk_val): 
     
    269269        (join_table, source_col_name), 
    270270        [source_pk_val]) 
    271     connection.commit() 
     271    transaction.commit_unless_managed() 
    272272 
    273273class ManyRelatedObjectsDescriptor(object): 
  • django/branches/magic-removal/django/db/models/query.py

    r2422 r2457  
    1 from django.db import backend, connection 
     1from django.db import backend, connection, transaction 
    22from django.db.models.fields import DateField, FieldDoesNotExist 
    33from django.db.models import signals 
     
    847847            dispatcher.send(signal=signals.post_delete, sender=cls, instance=instance) 
    848848 
    849     connection.commit() 
     849    transaction.commit_unless_managed() 
  • django/branches/magic-removal/docs/middleware.txt

    r1866 r2457  
    102102 
    103103.. _`session documentation`: http://www.djangoproject.com/documentation/sessions/ 
     104 
     105django.middleware.transaction.TransactionMiddleware 
     106--------------------------------------------------- 
     107 
     108Binds commit and rollback to the request/response phase. If a view function runs 
     109successfully, a commit is done. If it fails with an exception, a rollback is 
     110done. 
     111 
     112The order of this middleware in the stack is important: middleware modules 
     113running outside of it run with commit-on-save - the default Django behavior. 
     114Middleware modules running inside it (coming later in the stack) will be under 
     115the same transaction control as the view functions. 
     116 
     117See the `transaction management documentation`_. 
     118 
     119.. _`transaction management documentation`: http://www.djangoproject.com/documentation/transaction/ 
     120 
    104121 
    105122Writing your own middleware 
  • django/branches/magic-removal/tests/runtests.py

    r2387 r2457  
    4040 
    4141    def report_unexpected_exception(self, out, test, example, exc_info): 
    42         from django.db import connection 
     42        from django.db import transaction 
    4343        tb = ''.join(traceback.format_exception(*exc_info)[1:]) 
    4444        log_error(test.name, "API test raised an exception", 
     
    4646        # Rollback, in case of database errors. Otherwise they'd have 
    4747        # side effects on other tests. 
    48         connection.rollback() 
     48        transaction.rollback_unless_managed() 
    4949 
    5050normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)