def test_exception(self): transaction.enter_transaction_management() Band.objects.create(name='The Beatles') self.assertTrue(transaction.is_dirty()) TransactionMiddleware().process_exception(self.request, None) self.assertFalse(transaction.is_dirty()) self.assertEqual(Band.objects.count(), 0)
def wrapped_func(*args, **kwargs): enter_transaction_management(using=using) managed(True, using=using) try: res = func(*args, **kwargs) except: if is_dirty(using=using): rollback(using=using) raise else: if is_dirty(using=using): if not isinstance( res, HttpResponse ) or res.status_code < 200 or res.status_code >= 400: rollback(using=using) else: try: commit(using=using) except: rollback(using=using) raise finally: leave_transaction_management(using=using) return res
def main(): '''Read sys.argv for configuration, and perform the imports.''' if len(sys.argv) != 3: print "Usage: %s pootle.prefs users.prefs" % ( sys.argv[0]) return oldprefsfile = sys.argv[1] parsed_oldprefs = prefs.PrefsParser(oldprefsfile) usersfile = sys.argv[2] parsed_users = prefs.PrefsParser(usersfile) try: try: transaction.enter_transaction_management() transaction.managed(True) set_up_db_then_import_languages_then_users(parsed_oldprefs, parsed_users) except: if transaction.is_dirty(): transaction.rollback() if transaction.is_managed(): transaction.leave_transaction_management() raise finally: if transaction.is_managed(): if transaction.is_dirty(): transaction.commit() if transaction.is_managed(): transaction.leave_transaction_management()
def test_transaction_rollback(self): """Tests johnny's handling of transaction rollbacks. Similar to the commit, this sets up a write to a db in a transaction, reads from it (to force a cache write of sometime), then rolls back.""" from Queue import Queue as queue from django.db import transaction from testapp.models import Genre, Publisher from johnny import cache if settings.DATABASE_ENGINE == 'sqlite3': print "\n Skipping test requiring multiple threads." return self.failUnless(transaction.is_managed() == False) self.failUnless(transaction.is_dirty() == False) connection.queries = [] cache.local.clear() q = queue() other = lambda x: self._run_threaded(x, q) # load some data start = Genre.objects.get(id=1) other('Genre.objects.get(id=1)') hit, ostart = q.get() # these should be the same and should have hit cache self.failUnless(hit) self.failUnless(ostart == start) # enter manual transaction management transaction.enter_transaction_management() transaction.managed() start.title = 'Jackie Chan Novels' # local invalidation, this key should hit the localstore! nowlen = len(cache.local) start.save() self.failUnless(nowlen != len(cache.local)) # perform a read OUTSIDE this transaction... it should still see the # old gen key, and should still find the "old" data other('Genre.objects.get(id=1)') hit, ostart = q.get() self.failUnless(hit) self.failUnless(ostart.title != start.title) # perform a READ inside the transaction; this should hit the localstore # but not the outside! nowlen = len(cache.local) start2 = Genre.objects.get(id=1) self.failUnless(start2.title == start.title) self.failUnless(len(cache.local) > nowlen) transaction.rollback() # we rollback, and flush all johnny keys related to this transaction # subsequent gets should STILL hit the cache in the other thread # and indeed, in this thread. self.failUnless(transaction.is_dirty() == False) other('Genre.objects.get(id=1)') hit, ostart = q.get() self.failUnless(hit) start = Genre.objects.get(id=1) self.failUnless(ostart.title == start.title) transaction.managed(False) transaction.leave_transaction_management()
def _commit_on_success_unless_managed(*args, **kw): try: if transaction.is_managed(): forced_managed = False else: transaction.enter_transaction_management() forced_managed = True try: res = func(*args, **kw) except: # All exceptions must be handled here (even string ones). if transaction.is_dirty(): if forced_managed: transaction.rollback() else: transaction.rollback_unless_managed() raise else: if transaction.is_dirty(): if forced_managed: transaction.commit() else: transaction.commit_unless_managed() return res finally: if forced_managed: transaction.leave_transaction_management()
def wrapped_func(*args, **kwargs): enter_transaction_management(using=using) managed(True, using=using) try: res = func(*args, **kwargs) except: if is_dirty(using=using): rollback(using=using) raise else: if is_dirty(using=using): if not isinstance(res, HttpResponse) or res.status_code < 200 or res.status_code >= 400: rollback(using=using) else: try: commit(using=using) except: rollback(using=using) raise finally: leave_transaction_management(using=using) return res
def test_managed_response(self): transaction.enter_transaction_management() Band.objects.create(name="The Beatles") self.assertTrue(transaction.is_dirty()) TransactionMiddleware().process_response(self.request, self.response) self.assertFalse(transaction.is_dirty()) self.assertEqual(Band.objects.count(), 1)
def exiting(exc_type, using): signal = NO_SIGNAL try: if exc_type is not None: if transaction.is_dirty(using=using): transaction.rollback(using=using) signal = ROLLBACK_SIGNAL else: signal = COMMIT_SIGNAL if transaction.is_dirty(using=using): try: transaction.commit(using=using) except: transaction.rollback(using=using) signal = ROLLBACK_SIGNAL raise finally: transaction.leave_transaction_management(using=using) if signal == COMMIT_SIGNAL: transaction.signals._send_post_commit(using) elif signal == ROLLBACK_SIGNAL: transaction.signals._send_post_rollback(using) elif signal == SAVEPOINT_ROLLBACK_SIGNAL: transaction.signals._on_exit_without_update(using) elif signal == NO_SIGNAL: transaction.signals._on_exit_without_update(using)
def test_managed_response(self): transaction.enter_transaction_management() transaction.managed(True) Band.objects.create(name='The Beatles') self.assertTrue(transaction.is_dirty()) TransactionMiddleware().process_response(self.request, self.response) self.assertFalse(transaction.is_dirty()) self.assertEqual(Band.objects.count(), 1)
def migrate_model(processor, model, fields): from ella.core.models import Publishable model = get_model(*model.split('.')) ct = ContentType.objects.get_for_model(model) if model == Publishable: ct = None print 'processing', model._meta, ':', sys.stdout.flush() converted = 0 deps = 0 try: enter_transaction_management() managed(True) try: for m in model.objects.order_by().iterator(): if not ct: # publishable ct = ContentType.objects.get_for_id(m.content_type_id) sys.stdout.write('.') converted += 1 # commit every 1000 iterations if (converted % 1000) == 0 and is_dirty(): commit() sys.stdout.write('C') sys.stdout.flush() dirty = False for f in fields: val = getattr(m, f) if val: val, cnt = BOX_RE.subn(update_field(m, ct), val) if cnt > 0: deps += cnt setattr(m, f, val) dirty = True SourceText.objects.extract_from_instance(m, processor, fields, content_type=ct, force_save=dirty, force_create=True) except: # rollback and propagate if something goes wrong if is_dirty(): rollback() raise else: # commit at the end if is_dirty(): commit() finally: leave_transaction_management() print print 'DONE converted %d (%d reported dependencies)' % (converted, deps,) sys.stdout.flush()
def test_transaction_rollback(self): """Tests johnny's handling of transaction rollbacks. Similar to the commit, this sets up a write to a db in a transaction, reads from it (to force a cache write of sometime), then rolls back.""" if not is_multithreading_safe(db_using='default'): print("\n Skipping test requiring multiple threads.") return self.assertFalse(is_managed()) self.assertFalse(transaction.is_dirty()) cache.local.clear() q = Queue() other = lambda x: self._run_threaded(x, q) # load some data start = Genre.objects.get(id=1) other('Genre.objects.get(id=1)') hit, ostart = q.get() # these should be the same and should have hit cache self.assertTrue(hit) self.assertEqual(ostart, start) # enter manual transaction management transaction.enter_transaction_management() managed() start.title = 'Jackie Chan Novels' # local invalidation, this key should hit the localstore! nowlen = len(cache.local) start.save() self.assertNotEqual(nowlen, len(cache.local)) # perform a read OUTSIDE this transaction... it should still see the # old gen key, and should still find the "old" data other('Genre.objects.get(id=1)') hit, ostart = q.get() self.assertTrue(hit) self.assertNotEqual(ostart.title, start.title) # perform a READ inside the transaction; this should hit the localstore # but not the outside! nowlen = len(cache.local) start2 = Genre.objects.get(id=1) self.assertEqual(start2.title, start.title) self.assertTrue(len(cache.local) > nowlen) transaction.rollback() # we rollback, and flush all johnny keys related to this transaction # subsequent gets should STILL hit the cache in the other thread # and indeed, in this thread. self.assertFalse(transaction.is_dirty()) other('Genre.objects.get(id=1)') hit, ostart = q.get() self.assertTrue(hit) start = Genre.objects.get(id=1) self.assertEqual(ostart.title, start.title) managed(False) transaction.leave_transaction_management()
def commit(self): if transaction.is_dirty(): transaction.commit() while self.deserialized_objects: instance = self.deserialized_objects.popleft() instance.save() transaction.commit() self.deserialized_objects = None for instance in self: instance.save() transaction.commit() if transaction.is_dirty(): transaction.commit()
def test_0001(self): # надо для падчинга from celery_xa.patcher import celery_xa_patch @celery_xa_patch @task def add(a, b): return a + b self.assertEqual(transaction.is_dirty(), False) user = User(username = '******') user.save() self.assertEqual(transaction.is_dirty(), True) add.delay(2, 3)
def __exit__(self, exc_type, exc_value, traceback): try: if exc_type is not None: if t.is_dirty(using=self.using): t.rollback(using=self.using) else: if t.is_dirty(using=self.using): try: t.commit(using=self.using) except: t.rollback(using=self.using) raise finally: t.leave_transaction_management(using=self.using)
def apply_async(cls, *args, **kwargs): # Delay the task unless the client requested otherwise or transactions # aren't being managed (i.e. the signal handlers won't send the task). # A rather roundabout way of allowing control of transaction behaviour from source. I'm sure there's a better way. after_transaction = True if len(args) > 1: if isinstance(args[1], dict): after_transaction = args[1].pop('after_transaction', True) if 'after_transaction' in kwargs: after_transaction = kwargs.pop('after_transaction') if transaction.is_managed() and after_transaction: if not transaction.is_dirty(): # Always mark the transaction as dirty # because we push task in queue that must be fired or discarded if 'using' in kwargs: transaction.set_dirty(using=kwargs['using']) else: transaction.set_dirty() _get_task_queue().append((cls, args, kwargs)) else: apply_async_orig = cls.original_apply_async if current_app.conf.CELERY_ALWAYS_EAGER: apply_async_orig = transaction.autocommit()(apply_async_orig) return apply_async_orig(*args, **kwargs)
def process_exception(self, request, exception): """Rolls back the database and leaves transaction management.""" if transaction.is_managed(): if transaction.is_dirty(): transaction.rollback() transaction.leave_transaction_management()
def post(self, request): dataio = None success = False message = u"" data = u"" try: if self.check_basic_auth(): dataio = StringIO(request.read()) load_categories(get_text_from_data(dataio)) success = True message = u"Успешно загружены данные из 1С." return HttpResponse("success") else: error_message = u"Ошибка загрузки данных из 1С: неправильное имя пользователя или пароль." auth_message = u"HTTP_AUTHORIZATION: " + self.request.META.get('HTTP_AUTHORIZATION', ' ') success = False message = u"\n".join([error_message, auth_message]) return HttpResponse("failure", status=401) except Exception as e: if transaction.is_dirty(): transaction.rollback() success = False message = u"\n".join([u"Ошибка загрузки данных из 1С:", traceback.format_exc()]) if dataio: data = get_text_from_data(dataio) dataio.close() raise finally: with transaction.commit_on_success(): Log.objects.create(success=success, message=message, data=data)
def managed(old_function, *args, **kwargs): # Turning transaction management off causes the current transaction to be # committed if it's dirty. We must send the signal after the actual commit. flag = kwargs.get('flag', args[0] if args else None) if state is not None: using = kwargs.get('using', args[1] if len(args) > 1 else None) # Do not commit too early for prior versions of Django 1.3 thread_ident = thread.get_ident() top = state.get(thread_ident, {}).get(using, None) commit = top and not flag and transaction.is_dirty() else: commit = not flag and transaction.is_dirty() old_function(*args, **kwargs) if commit: transaction.signals.post_commit.send(None)
def _post_teardown(self): _post_teardown(self) super(TransactionQueryCacheBase, self)._post_teardown() if transaction.is_dirty(): transaction.rollback() if is_managed(): managed(False)
def test_savepoint_rollback(self): """Tests rollbacks of savepoints""" if not connection.features.uses_savepoints or connection.vendor == 'sqlite': return self.assertFalse(is_managed()) self.assertFalse(transaction.is_dirty()) cache.local.clear() managed() g = Genre.objects.get(pk=1) start_title = g.title g.title = "Adventures in Savepoint World" g.save() g = Genre.objects.get(pk=1) self.assertEqual(g.title, "Adventures in Savepoint World") sid = transaction.savepoint() g.title = "In the Void" g.save() g = Genre.objects.get(pk=1) self.assertEqual(g.title, "In the Void") transaction.savepoint_rollback(sid) g = Genre.objects.get(pk=1) self.assertEqual(g.title, "Adventures in Savepoint World") transaction.rollback() g = Genre.objects.get(pk=1) self.assertEqual(g.title, start_title)
def __exit__(self, exc_type, exc_value, traceback): if dtransaction.is_dirty(): if exc_value: dtransaction.rollback() else: dtransaction.commit() dtransaction.leave_transaction_management()
def apply_async(self, *args, **kwargs): # Delay the task unless the client requested otherwise or transactions # aren't being managed (i.e. the signal handlers won't send the task). if django.VERSION < (1, 6): if transaction.is_managed() and not current_app.conf.CELERY_ALWAYS_EAGER: if not transaction.is_dirty(): # Always mark the transaction as dirty # because we push task in queue that must be fired or discarded if 'using' in kwargs: transaction.set_dirty(using=kwargs['using']) else: transaction.set_dirty() _get_task_queue().append((self, args, kwargs)) else: apply_async_orig = super(PostTransactionTask, self).apply_async return apply_async_orig(*args, **kwargs) else: connection = get_connection() if connection.in_atomic_block and not getattr(current_app.conf, 'CELERY_ALWAYS_EAGER', False): _get_task_queue().append((self, args, kwargs)) else: return self.original_apply_async(*args, **kwargs)
def process_response(self, request, response): """Commits and leaves transaction management.""" if transaction.is_managed(): if transaction.is_dirty(): transaction.commit() transaction.leave_transaction_management() return response
def sentry_exception_handler(sender, request=None, **kwargs): try: exc_type, exc_value, exc_traceback = sys.exc_info() if conf.DEBUG or getattr(exc_type, 'skip_sentry', False): return if transaction.is_dirty(): transaction.rollback() if request: data = dict( META=request.META, POST=request.POST, GET=request.GET, COOKIES=request.COOKIES, ) else: data = dict() extra = dict( url=request and request.build_absolute_uri() or None, data=data, ) client = get_client() client.create_from_exception(**extra) except Exception, exc: try: logger.exception(u'Unable to process log entry: %s' % (exc,)) except Exception, exc: warnings.warn(u'Unable to process log entry: %s' % (exc,))
def handle(self, *args, **options): """ Upgrades the database. Executes SQL scripts that haven't already been applied to the database. """ self.do_list = options.get("do_list") self.do_execute = options.get("do_execute") self.do_create = options.get("do_create") self.do_seed = options.get("do_seed") self.args = args self.path = options.get("path") self.verbosity = int(options.get("verbosity", 1)) self.interactive = options.get("interactive") self.db = options.get("database", DEFAULT_DB_ALIAS) self.init_nashvegas() if self.do_create: self.create_migrations() if self.do_execute: self.execute_migrations(show_traceback=True) if self.do_list: self.list_migrations() if self.do_seed: self.seed_migrations() if transaction.is_dirty(): transaction.commit()
def sentry_exception_handler(request=None, **kwargs): try: exc_type, exc_value, exc_traceback = sys.exc_info() if conf.DEBUG or getattr(exc_type, 'skip_sentry', False): return if transaction.is_dirty(): transaction.rollback() extra = dict( request=request, ) message_id = get_client().create_from_exception(**extra) if request: # attach the sentry object to the request request.sentry = { 'id': message_id, } except Exception, exc: try: logger.exception(u'Unable to process log entry: %s' % (exc,)) except Exception, exc: warnings.warn(u'Unable to process log entry: %s' % (exc,))
def test_savepoint_rollback(self): """Tests rollbacks of savepoints""" if not connection.features.uses_savepoints: return self.assertFalse(is_managed()) self.assertFalse(transaction.is_dirty()) cache.local.clear() managed() transaction.enter_transaction_management() g = Genre.objects.get(pk=1) start_title = g.title g.title = "Adventures in Savepoint World" g.save() g = Genre.objects.get(pk=1) self.assertEqual(g.title, "Adventures in Savepoint World") sid = transaction.savepoint() g.title = "In the Void" g.save() g = Genre.objects.get(pk=1) self.assertEqual(g.title, "In the Void") transaction.savepoint_rollback(sid) g = Genre.objects.get(pk=1) self.assertEqual(g.title, "Adventures in Savepoint World") transaction.rollback() g = Genre.objects.get(pk=1) self.assertEqual(g.title, start_title)
def test_savepoint_rollback_sqlite(self): """SQLite savepoints in Django 1.6 don't work correctly with autocommit disabled, so we have to use transaction.atomic(). See https://docs.djangoproject.com/en/dev/topics/db/transactions/#savepoints-in-sqlite SQLite doesn't seem to support savepoints in Django < 1.6""" if not connection.features.uses_savepoints or connection.vendor != 'sqlite': return self.assertFalse(is_managed()) self.assertFalse(transaction.is_dirty()) cache.local.clear() try: with transaction.atomic(): g = Genre.objects.get(pk=1) start_title = g.title g.title = "Adventures in Savepoint World" g.save() g = Genre.objects.get(pk=1) self.assertEqual(g.title, "Adventures in Savepoint World") sid = transaction.savepoint() g.title = "In the Void" g.save() g = Genre.objects.get(pk=1) self.assertEqual(g.title, "In the Void") transaction.savepoint_rollback(sid) g = Genre.objects.get(pk=1) self.assertEqual(g.title, "Adventures in Savepoint World") raise IntegrityError('Exit transaction') except IntegrityError: pass g = Genre.objects.get(pk=1) self.assertEqual(g.title, start_title)
def on_shutter(self, state, commit_every=100): if not state.event_count and transaction.is_dirty(): transaction.commit() return def _handle_tasks(): for i, task in enumerate(state.tasks.items()): self.handle_task(task) if not i % commit_every: transaction.commit() for worker in state.workers.items(): self.handle_worker(worker) _handle_tasks() if transaction.is_dirty(): transaction.commit()
def sentry_exception_handler(sender, request=None, **kwargs): try: exc_type, exc_value, exc_traceback = sys.exc_info() if not settings.CATCH_404_ERRORS and issubclass(exc_type, Http404): return if settings.DEBUG or getattr(exc_type, "skip_sentry", False): return if transaction.is_dirty(): transaction.rollback() if request: data = dict(META=request.META, POST=request.POST, GET=request.GET, COOKIES=request.COOKIES) else: data = dict() extra = dict(url=request and request.build_absolute_uri() or None, data=data) client = get_client() client.create_from_exception(**extra) except Exception, exc: try: logger.exception(u"Unable to process log entry: %s" % (exc,)) except Exception, exc: warnings.warn(u"Unable to process log entry: %s" % (exc,))
def managed(flag=True, **kwargs): to_commit = False if not flag and transaction.is_dirty(): to_commit = True old_managed(flag=flag, **kwargs) if to_commit: transaction.signals._send_post_commit()
def managed(flag=True): to_commit = False if not flag and transaction.is_dirty(): to_commit = True old_managed(flag) if to_commit: transaction.signals._send_post_commit()
def tearDown(self): from django.db import transaction if transaction.is_managed(): if transaction.is_dirty(): transaction.rollback() transaction.managed(False) transaction.leave_transaction_management()
def test_savepoint_rollback(self): """Tests rollbacks of savepoints""" from django.db import transaction from testapp.models import Genre, Publisher from johnny import cache if not connection.features.uses_savepoints: return self.failUnless(transaction.is_managed() == False) self.failUnless(transaction.is_dirty() == False) connection.queries = [] cache.local.clear() transaction.enter_transaction_management() transaction.managed() g = Genre.objects.get(pk=1) start_title = g.title g.title = "Adventures in Savepoint World" g.save() g = Genre.objects.get(pk=1) self.failUnless(g.title == "Adventures in Savepoint World") sid = transaction.savepoint() g.title = "In the Void" g.save() g = Genre.objects.get(pk=1) self.failUnless(g.title == "In the Void") transaction.savepoint_rollback(sid) g = Genre.objects.get(pk=1) self.failUnless(g.title == "Adventures in Savepoint World") transaction.rollback() g = Genre.objects.get(pk=1) self.failUnless(g.title == start_title) transaction.managed(False) transaction.leave_transaction_management()
def apply_async(self, *args, **kwargs): # Delay the task unless the client requested otherwise or transactions # aren't being managed (i.e. the signal handlers won't send the task). celery_eager = _get_celery_settings('CELERY_ALWAYS_EAGER') # New setting to run eager task post transaction # defaults to `not CELERY_ALWAYS_EAGER` eager_transaction = _get_celery_settings('CELERY_EAGER_TRANSACTION', not celery_eager) if django.VERSION < (1, 6): if transaction.is_managed() and eager_transaction: if not transaction.is_dirty(): # Always mark the transaction as dirty # because we push task in queue that must be fired or discarded if 'using' in kwargs: transaction.set_dirty(using=kwargs['using']) else: transaction.set_dirty() _get_task_queue().append((self, args, kwargs)) else: apply_async_orig = super(PostTransactionTask, self).apply_async return apply_async_orig(*args, **kwargs) else: connection = get_connection() if connection.in_atomic_block and eager_transaction: _get_task_queue().append((self, args, kwargs)) else: return self.original_apply_async(*args, **kwargs)
def exception_handler(request=None, **kwargs): logger = logging.getLogger('peavy.signals.exception_handler') if transaction.is_dirty(): transaction.rollback() logger.exception('Exception: {0}'.format(exc_type), exc_info=True)
def managed(flag=True, **kw): to_commit = False if not flag and transaction.is_dirty(): to_commit = True old_managed(flag=flag, **kw) if to_commit: transaction.signals._send_post_commit() #@UndefinedVariable
def process_response(self, request, response): """Commits and leaves transaction management.""" if transaction.is_managed(using=self.get_tenant(request)): if transaction.is_dirty(using=self.get_tenant(request)): transaction.commit(using=self.get_tenant(request)) transaction.leave_transaction_management(using=self.get_tenant(request)) return response
def managed(old_function, flag=True, using=None): # Turning transaction management off causes the current transaction to be # committed if it's dirty. We must send the signal after the actual commit. commit = not flag and transaction.is_dirty() old_function(flag, using) if commit: transaction.signals.post_commit.send(None)
def test_failing_commit(self): # It is possible that connection.commit() fails. Check that # TransactionMiddleware handles such cases correctly. try: def raise_exception(): raise IntegrityError() connections[DEFAULT_DB_ALIAS].commit = raise_exception transaction.enter_transaction_management() Band.objects.create(name='The Beatles') self.assertTrue(transaction.is_dirty()) with self.assertRaises(IntegrityError): TransactionMiddleware().process_response(self.request, None) self.assertFalse(transaction.is_dirty()) self.assertEqual(Band.objects.count(), 0) self.assertFalse(transaction.is_managed()) finally: del connections[DEFAULT_DB_ALIAS].commit
def leave_transaction_management(old_function, using=None): # If the transaction is dirty, it is rolled back and an exception is # raised. We need to send the rollback signal before that happens. if transaction.is_dirty(): transaction.signals.post_rollback.send(None) old_function(using) transaction.signals.post_transaction_management.send(None)
def test_transaction_dirty_managed(self): """ Check that a select_for_update sets the transaction to be dirty when executed under txn management. Setting the txn dirty means that it will be either committed or rolled back by Django, which will release any locks held by the SELECT FOR UPDATE. """ people = list(Person.objects.select_for_update()) self.assertTrue(transaction.is_dirty())
def call(*args, **kwargs): try: return fn(*args, **kwargs) finally: if transaction.is_dirty(): transaction.commit() db.connections.all()[0].close() db.close_old_connections()
def transaction_commit(using): """ DB(using)がdirtyな場合、commitし、トランザクション区間を終了する。 """ if (transaction.is_managed(using=using)): if (transaction.is_dirty(using=using)): transaction.commit(using=using) transaction.leave_transaction_management(using=using)
def test_transaction_not_dirty_unmanaged(self): """ If we're not under txn management, the txn will never be marked as dirty. """ transaction.managed(False) transaction.leave_transaction_management() people = list(Person.objects.select_for_update()) self.assertFalse(transaction.is_dirty())
def process_response(self, request, response): """Commits and leaves transaction management.""" db = self.get_tenant(request) if db: if transaction.is_managed(using=db): if transaction.is_dirty(using=db): transaction.commit(using=db) transaction.leave_transaction_management(using=db) return response
def test_enter_autocommit(self): transaction.enter_transaction_management() self.assertFalse(connection.autocommit) self.assertEqual(connection.isolation_level, self._serializable) list(Mod.objects.all()) self.assertTrue(transaction.is_dirty()) # Enter autocommit mode again. transaction.enter_transaction_management(False) self.assertFalse(transaction.is_dirty()) self.assertEqual(connection.connection.get_transaction_status(), self._idle) list(Mod.objects.all()) self.assertFalse(transaction.is_dirty()) transaction.leave_transaction_management() self.assertFalse(connection.autocommit) self.assertEqual(connection.isolation_level, self._serializable) transaction.leave_transaction_management() self.assertTrue(connection.autocommit)
def tearDownClass(cls): """Truncate the world, and turn manual commit management back off.""" cls._fixture_teardown() for db in cls._databases(): # Finish off any transactions that may have happened in # tearDownClass in a child method. if transaction.is_dirty(using=db): transaction.commit(using=db) transaction.leave_transaction_management(using=db)
def process_exception(self, request, exception): """Rolls back the database and leaves transaction management""" if transaction.is_dirty(): # This rollback might fail because of network failure for example. # If rollback isn't possible it is impossible to clean the # connection's state. So leave the connection in dirty state and # let request_finished signal deal with cleaning the connection. transaction.rollback() transaction.leave_transaction_management()
def __enter__(self): self.was_dirty = dtransaction.is_dirty() self.old_connection = connection.connection connection.connection = self.connection dtransaction.enter_transaction_management() dtransaction.managed(True) dtransaction.set_clean()