Changeset 2457
- Timestamp:
- 03/01/06 11:08:33 (3 years ago)
- Files:
-
- django/branches/magic-removal/django/bin/daily_cleanup.py (modified) (2 diffs)
- django/branches/magic-removal/django/conf/global_settings.py (modified) (1 diff)
- django/branches/magic-removal/django/core/cache/backends/db.py (modified) (3 diffs)
- django/branches/magic-removal/django/core/management.py (modified) (13 diffs)
- django/branches/magic-removal/django/db/backends/ado_mssql/base.py (modified) (1 diff)
- django/branches/magic-removal/django/db/backends/dummy/base.py (modified) (1 diff)
- django/branches/magic-removal/django/db/backends/mysql/base.py (modified) (1 diff)
- django/branches/magic-removal/django/db/backends/postgresql/base.py (modified) (1 diff)
- django/branches/magic-removal/django/db/backends/sqlite3/base.py (modified) (2 diffs)
- django/branches/magic-removal/django/db/__init__.py (modified) (1 diff)
- django/branches/magic-removal/django/db/models/base.py (modified) (4 diffs)
- django/branches/magic-removal/django/db/models/fields/related.py (modified) (4 diffs)
- django/branches/magic-removal/django/db/models/query.py (modified) (2 diffs)
- django/branches/magic-removal/django/db/transaction.py (added)
- django/branches/magic-removal/django/middleware/transaction.py (added)
- django/branches/magic-removal/docs/middleware.txt (modified) (1 diff)
- django/branches/magic-removal/docs/transactions.txt (added)
- django/branches/magic-removal/tests/modeltests/transactions (added)
- django/branches/magic-removal/tests/modeltests/transactions/__init__.py (added)
- django/branches/magic-removal/tests/modeltests/transactions/models.py (added)
- django/branches/magic-removal/tests/runtests.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/django/bin/daily_cleanup.py
r1638 r2457 1 1 "Daily cleanup file" 2 2 3 from django.db import backend, connection 3 from django.db import backend, connection, transaction 4 4 5 5 DOCUMENTATION_DIRECTORY = '/home/html/documentation/' … … 12 12 cursor.execute("DELETE FROM %s WHERE %s < NOW() - INTERVAL '1 week'" % \ 13 13 (backend.quote_name('registration_challenges'), backend.quote_name('request_date'))) 14 connection.commit()14 transaction.commit_unless_managed() 15 15 16 16 if __name__ == "__main__": django/branches/magic-removal/django/conf/global_settings.py
r2295 r2457 195 195 ENABLE_PSYCO = False 196 196 197 # Do you want to manage transactions manually? 198 # Hint: you really don't! 199 TRANSACTIONS_MANAGED = False 200 197 201 ############## 198 202 # MIDDLEWARE # django/branches/magic-removal/django/core/cache/backends/db.py
r2388 r2457 2 2 3 3 from django.core.cache.backends.base import BaseCache 4 from django.db import connection 4 from django.db import connection, transaction 5 5 import base64, time 6 6 from datetime import datetime … … 34 34 if row[2] < now: 35 35 cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) 36 connection.commit()36 transaction.commit_unless_managed() 37 37 return default 38 38 return pickle.loads(base64.decodestring(row[1])) … … 59 59 pass 60 60 else: 61 connection.commit()61 transaction.commit_unless_managed() 62 62 63 63 def delete(self, key): 64 64 cursor = connection.cursor() 65 65 cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) 66 connection.commit()66 transaction.commit_unless_managed() 67 67 68 68 def has_key(self, key): django/branches/magic-removal/django/core/management.py
r2452 r2457 219 219 def get_sql_delete(app): 220 220 "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 222 222 223 223 try: … … 234 234 except: 235 235 # The table doesn't exist, so it doesn't need to be dropped. 236 connection.rollback()236 transaction.rollback_unless_managed() 237 237 admin_log_exists = False 238 238 else: … … 253 253 except: 254 254 # The table doesn't exist, so it doesn't need to be dropped. 255 connection.rollback()255 transaction.rollback_unless_managed() 256 256 else: 257 257 opts = klass._meta … … 269 269 except: 270 270 # The table doesn't exist, so it doesn't need to be dropped. 271 connection.rollback()271 transaction.rollback_unless_managed() 272 272 else: 273 273 output.append("DROP TABLE %s;" % backend.quote_name(klass._meta.db_table)) … … 291 291 cursor.execute("SELECT 1 FROM %s LIMIT 1" % backend.quote_name(f.m2m_db_table())) 292 292 except: 293 connection.rollback()293 transaction.rollback_unless_managed() 294 294 else: 295 295 output.append("DROP TABLE %s;" % backend.quote_name(f.m2m_db_table())) … … 404 404 def syncdb(): 405 405 "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_module406 from django.db import backend, connection, transaction, models, get_creation_module, get_introspection_module 407 407 introspection_module = get_introspection_module() 408 408 data_types = get_creation_module().DATA_TYPES … … 471 471 cursor.execute(sql) 472 472 473 connection.commit()473 transaction.commit_unless_managed() 474 474 syncdb.args = '' 475 475 … … 500 500 def install(app): 501 501 "Executes the equivalent of 'get_sql_all' in the current database." 502 from django.db import connection 502 from django.db import connection, transaction 503 503 from cStringIO import StringIO 504 504 app_name = app.__name__[app.__name__.rindex('.')+1:] … … 527 527 The full error: %s\n""" % \ 528 528 (app_name, app_label, e)) 529 connection.rollback()529 transaction.rollback_unless_managed() 530 530 sys.exit(1) 531 connection.commit()531 transaction.commit_unless_managed() 532 532 install.help_doc = "Executes ``sqlall`` for the given app(s) in the current database." 533 533 install.args = APP_ARGS … … 535 535 def reset(app): 536 536 "Executes the equivalent of 'get_sql_reset' in the current database." 537 from django.db import connection 537 from django.db import connection, transaction 538 538 from cStringIO import StringIO 539 539 app_name = app.__name__[app.__name__.rindex('.')+1:] … … 569 569 The full error: %s\n""" % \ 570 570 (app_name, app_label, e)) 571 connection.rollback()571 transaction.rollback_unless_managed() 572 572 sys.exit(1) 573 connection.commit()573 transaction.commit_unless_managed() 574 574 else: 575 575 print "Reset cancelled." … … 1036 1036 def createcachetable(tablename): 1037 1037 "Creates the table needed to use the SQL cache backend" 1038 from django.db import backend, get_creation_module, models1038 from django.db import backend, connection, transaction, get_creation_module, models 1039 1039 data_types = get_creation_module().DATA_TYPES 1040 1040 fields = ( … … 1067 1067 for statement in index_output: 1068 1068 curs.execute(statement) 1069 connection.commit()1069 transaction.commit_unless_managed() 1070 1070 createcachetable.args = "[tablename]" 1071 1071 django/branches/magic-removal/django/db/backends/ado_mssql/base.py
r2422 r2457 65 65 return cursor 66 66 67 def commit(self):67 def _commit(self): 68 68 return self.connection.commit() 69 69 70 def rollback(self):70 def _rollback(self): 71 71 if self.connection: 72 72 return self.connection.rollback() django/branches/magic-removal/django/db/backends/dummy/base.py
r1803 r2457 18 18 class DatabaseWrapper: 19 19 cursor = complain 20 commit = complain21 rollback = complain20 _commit = complain 21 _rollback = complain 22 22 23 23 def close(self): django/branches/magic-removal/django/db/backends/mysql/base.py
r2451 r2457 72 72 return cursor 73 73 74 def commit(self):74 def _commit(self): 75 75 self.connection.commit() 76 76 77 def rollback(self):77 def _rollback(self): 78 78 if self.connection: 79 79 try: django/branches/magic-removal/django/db/backends/postgresql/base.py
r2422 r2457 38 38 return cursor 39 39 40 def commit(self):40 def _commit(self): 41 41 return self.connection.commit() 42 42 43 def rollback(self):43 def _rollback(self): 44 44 if self.connection: 45 45 return self.connection.rollback() django/branches/magic-removal/django/db/backends/sqlite3/base.py
r2422 r2457 40 40 return cursor 41 41 42 def commit(self):42 def _commit(self): 43 43 self.connection.commit() 44 44 45 def rollback(self):45 def _rollback(self): 46 46 if self.connection: 47 47 self.connection.rollback() … … 68 68 69 69 def convert_query(self, query, num_params): 70 # XXX this seems too simple to be correct... is this right?71 70 return query % tuple("?" * num_params) 72 71 django/branches/magic-removal/django/db/__init__.py
r1999 r2457 39 39 # Register an event that rolls back the connection 40 40 # when a Django request has an exception. 41 dispatcher.connect(lambda: connection.rollback(), signal=signals.got_request_exception) 41 def _rollback_on_exception(): 42 from django.db import transaction 43 transaction.rollback_unless_managed() 44 dispatcher.connect(_rollback_on_exception, signal=signals.got_request_exception) django/branches/magic-removal/django/db/models/base.py
r2438 r2457 6 6 from django.db.models.query import orderlist2sql, delete_objects 7 7 from django.db.models.options import Options, AdminOptions 8 from django.db import connection, backend 8 from django.db import connection, backend, transaction 9 9 from django.db.models import signals 10 10 from django.db.models.loading import register_models … … 185 185 if self._meta.has_auto_field and not pk_set: 186 186 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() 188 188 189 189 # Run any post-save hooks. … … 341 341 backend.quote_name(rel_field.m2m_reverse_name())) 342 342 cursor.executemany(sql, [(this_id, i) for i in id_list]) 343 connection.commit()343 transaction.commit_unless_managed() 344 344 345 345 ############################################ … … 358 358 rel_val = getattr(self, ordered_obj.order_with_respect_to.rel.field_name) 359 359 cursor.executemany(sql, [(i, rel_val, j) for i, j in enumerate(id_list)]) 360 connection.commit()360 transaction.commit_unless_managed() 361 361 362 362 def 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 1 from django.db import backend, connection, transaction 2 2 from django.db.models import signals 3 3 from django.db.models.fields import AutoField, Field, IntegerField … … 232 232 (join_table, source_col_name, target_col_name), 233 233 [source_pk_val, obj_id]) 234 connection.commit()234 transaction.commit_unless_managed() 235 235 236 236 def _remove_m2m_items(rel_model, join_table, source_col_name, … … 256 256 (join_table, source_col_name, target_col_name), 257 257 [source_pk_val, obj._get_pk_val()]) 258 connection.commit()258 transaction.commit_unless_managed() 259 259 260 260 def _clear_m2m_items(join_table, source_col_name, source_pk_val): … … 269 269 (join_table, source_col_name), 270 270 [source_pk_val]) 271 connection.commit()271 transaction.commit_unless_managed() 272 272 273 273 class ManyRelatedObjectsDescriptor(object): django/branches/magic-removal/django/db/models/query.py
r2422 r2457 1 from django.db import backend, connection 1 from django.db import backend, connection, transaction 2 2 from django.db.models.fields import DateField, FieldDoesNotExist 3 3 from django.db.models import signals … … 847 847 dispatcher.send(signal=signals.post_delete, sender=cls, instance=instance) 848 848 849 connection.commit()849 transaction.commit_unless_managed() django/branches/magic-removal/docs/middleware.txt
r1866 r2457 102 102 103 103 .. _`session documentation`: http://www.djangoproject.com/documentation/sessions/ 104 105 django.middleware.transaction.TransactionMiddleware 106 --------------------------------------------------- 107 108 Binds commit and rollback to the request/response phase. If a view function runs 109 successfully, a commit is done. If it fails with an exception, a rollback is 110 done. 111 112 The order of this middleware in the stack is important: middleware modules 113 running outside of it run with commit-on-save - the default Django behavior. 114 Middleware modules running inside it (coming later in the stack) will be under 115 the same transaction control as the view functions. 116 117 See the `transaction management documentation`_. 118 119 .. _`transaction management documentation`: http://www.djangoproject.com/documentation/transaction/ 120 104 121 105 122 Writing your own middleware django/branches/magic-removal/tests/runtests.py
r2387 r2457 40 40 41 41 def report_unexpected_exception(self, out, test, example, exc_info): 42 from django.db import connection42 from django.db import transaction 43 43 tb = ''.join(traceback.format_exception(*exc_info)[1:]) 44 44 log_error(test.name, "API test raised an exception", … … 46 46 # Rollback, in case of database errors. Otherwise they'd have 47 47 # side effects on other tests. 48 connection.rollback()48 transaction.rollback_unless_managed() 49 49 50 50 normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)
