def test_request_finished_db_state(self): # Make sure there is an open connection connection.cursor() connection.enter_transaction_management() connection.managed(True) signals.request_finished.send(sender=self.__class__) # In-memory sqlite doesn't actually close connections. if connection.vendor != 'sqlite': self.assertIs(connection.connection, None) self.assertEqual(len(connection.transaction_state), 0)
def test_request_finished_db_state(self): # Make sure there is an open connection connection.cursor() connection.enter_transaction_management() connection.managed(True) signals.request_finished.send(sender=self.__class__) # In-memory sqlite doesn't actually close connections. if connection.vendor != 'sqlite': self.assertIs(connection.connection, None) self.assertEqual(len(connection.transaction_state), 0)
def test_request_finished_db_state(self): # Force closing connection on request end connection.settings_dict['CONN_MAX_AGE'] = 0 # The GET below will not succeed, but it will give a response with # defined ._handler_class. That is needed for sending the # request_finished signal. response = self.client.get('/') # Make sure there is an open connection connection.cursor() connection.enter_transaction_management() signals.request_finished.send(sender=response._handler_class) self.assertEqual(len(connection.transaction_state), 0)
def test_request_finished_db_state(self): # Force closing connection on request end connection.settings_dict['CONN_MAX_AGE'] = 0 # The GET below will not succeed, but it will give a response with # defined ._handler_class. That is needed for sending the # request_finished signal. response = self.client.get('/') # Make sure there is an open connection connection.cursor() connection.enter_transaction_management() signals.request_finished.send(sender=response._handler_class) self.assertEqual(len(connection.transaction_state), 0)
def test_request_finished_db_state(self): # The GET below will not succeed, but it will give a response with # defined ._handler_class. That is needed for sending the # request_finished signal. response = self.client.get('/') # Make sure there is an open connection connection.cursor() connection.enter_transaction_management() connection.managed(True) signals.request_finished.send(sender=response._handler_class) # In-memory sqlite doesn't actually close connections. if connection.vendor != 'sqlite': self.assertIs(connection.connection, None) self.assertEqual(len(connection.transaction_state), 0)
def test_commit_unless_managed_in_managed(self): cursor = connection.cursor() connection.enter_transaction_management() transaction.managed(True) cursor.execute("INSERT into transactions_regress_mod (fld) values (2)") connection.commit_unless_managed() self.assertTrue(connection.is_dirty()) connection.rollback() self.assertFalse(connection.is_dirty()) self.assertEqual(len(Mod.objects.all()), 0) connection.commit() connection.leave_transaction_management() self.assertFalse(connection.is_dirty()) self.assertEqual(len(Mod.objects.all()), 0) self.assertTrue(connection.is_dirty()) connection.commit_unless_managed() self.assertFalse(connection.is_dirty()) self.assertEqual(len(Mod.objects.all()), 0)
def set_search_path(self, cursor): """ Actual search_path modification for the cursor. Database will search schemata from left to right when looking for the object (table, index, sequence, etc.). """ if self.schema_name is None: raise ImproperlyConfigured("Database schema not set. Did your forget " "to call set_schema() or set_tenant()?") _check_identifier(self.schema_name) connection.enter_transaction_management() try: if self.schema_name == 'public': cursor.execute('SET search_path = public') else: cursor.execute('SET search_path = %s', [self.schema_name]) #, public except utils.DatabaseError, e: connection.rollback() raise utils.DatabaseError(e.message)
def test_request_finished_failed_connection(self): # Force closing connection on request end connection.settings_dict['CONN_MAX_AGE'] = 0 connection.enter_transaction_management() connection.set_dirty() # Test that the rollback doesn't succeed (for example network failure # could cause this). def fail_horribly(): raise Exception("Horrible failure!") connection._rollback = fail_horribly try: with self.assertRaises(Exception): signals.request_finished.send(sender=self.__class__) # The connection's state wasn't cleaned up self.assertEqual(len(connection.transaction_state), 1) finally: del connection._rollback # The connection will be cleaned on next request where the conn # works again. signals.request_finished.send(sender=self.__class__) self.assertEqual(len(connection.transaction_state), 0)
def test_request_finished_failed_connection(self): # Force closing connection on request end connection.settings_dict['CONN_MAX_AGE'] = 0 connection.enter_transaction_management() connection.set_dirty() # Test that the rollback doesn't succeed (for example network failure # could cause this). def fail_horribly(): raise Exception("Horrible failure!") connection._rollback = fail_horribly try: with self.assertRaises(Exception): signals.request_finished.send(sender=self.__class__) # The connection's state wasn't cleaned up self.assertEqual(len(connection.transaction_state), 1) finally: del connection._rollback # The connection will be cleaned on next request where the conn # works again. signals.request_finished.send(sender=self.__class__) self.assertEqual(len(connection.transaction_state), 0)
def main(self, batch_size=None, sleep=0): """Invoked from the management command to do all the work.""" batch_size = batch_size or self.BATCH_SIZE connection.enter_transaction_management() self.last_student_module_id = self.get_last_student_module_id() self.load_state() while self.next_student_module_id <= self.last_student_module_id: for smid in self.module_ids_to_check(batch_size): try: self.clean_one_student_module(smid) except Exception: # pylint: disable=W0703 trace = traceback.format_exc() self.say("Couldn't clean student_module_id {}:\n{}".format(smid, trace)) if not self.dry_run: self.commit() self.save_state() if sleep: time.sleep(sleep)
def main(self, batch_size=None, sleep=0): """Invoked from the management command to do all the work.""" batch_size = batch_size or self.BATCH_SIZE connection.enter_transaction_management() self.last_student_module_id = self.get_last_student_module_id() self.load_state() while self.next_student_module_id <= self.last_student_module_id: for smid in self.module_ids_to_check(batch_size): try: self.clean_one_student_module(smid) except Exception: # pylint: disable=broad-except trace = traceback.format_exc() self.say("Couldn't clean student_module_id {}:\n{}".format( smid, trace)) if not self.dry_run: self.commit() self.save_state() if sleep: time.sleep(sleep)
def test_enter_exit_management(self): orig_dirty = connection._dirty connection.enter_transaction_management() connection.leave_transaction_management() self.assertEqual(orig_dirty, connection._dirty)
def test_enter_exit_management(self): orig_dirty = connection._dirty connection.enter_transaction_management() connection.leave_transaction_management() self.assertEqual(orig_dirty, connection._dirty)