示例#1
0
 def other_thread():
     try:
         with transaction.atomic():
             Reporter.objects.select_for_update().get(id=1)
             main_thread_ready.wait()
             # 1) This line locks... (see below for 2)
             Reporter.objects.exclude(id=1).update(id=2)
     finally:
         # This is the thread-local connection, not the main connection.
         connection.close()
示例#2
0
    def test_hooks_cleared_on_reconnect(self):
        with transaction.atomic():
            self.do(1)
            connection.close()

        connection.connect()

        with transaction.atomic():
            self.do(2)

        self.assertDone([2])
示例#3
0
    def test_signal(self):
        data = {}

        def receiver(sender, connection, **kwargs):
            data["connection"] = connection

        connection_created.connect(receiver)
        connection.close()
        connection.cursor()
        self.assertIs(data["connection"].connection, connection.connection)

        connection_created.disconnect(receiver)
        data.clear()
        connection.cursor()
        self.assertEqual(data, {})
示例#4
0
 def test_atomic_prevents_queries_in_broken_transaction_after_client_close(
         self):
     with transaction.atomic():
         Reporter.objects.create(first_name="Archibald",
                                 last_name="Haddock")
         connection.close()
         # The connection is closed and the transaction is marked as
         # needing rollback. This will raise an InterfaceError on databases
         # that refuse to create cursors on closed connections (PostgreSQL)
         # and a TransactionManagementError on other databases.
         with self.assertRaises(Error):
             Reporter.objects.create(first_name="Cuthbert",
                                     last_name="Calculus")
     # The connection is usable again .
     self.assertEqual(Reporter.objects.count(), 0)
示例#5
0
 def test_is_usable_after_database_disconnects(self):
     """
     is_usable() doesn't crash when the database disconnects (#21553).
     """
     # Open a connection to the database.
     with connection.cursor():
         pass
     # Emulate a connection close by the database.
     connection._close()
     # Even then is_usable() should not raise an exception.
     try:
         self.assertFalse(connection.is_usable())
     finally:
         # Clean up the mess created by connection._close(). Since the
         # connection is already closed, this crashes on some backends.
         try:
             connection.close()
         except Exception:
             pass
示例#6
0
 def raw(status):
     try:
         list(
             Person.objects.raw(
                 'SELECT * FROM %s %s' % (
                     Person._meta.db_table,
                     connection.ops.for_update_sql(nowait=True)
                 )
             )
         )
     except DatabaseError as e:
         status.append(e)
     finally:
         # This method is run in a separate thread. It uses its own
         # database connection. Close it without waiting for the GC.
         # Connection cannot be closed on Oracle because cursor is still
         # open.
         if connection.vendor != 'oracle':
             connection.close()
示例#7
0
    def run_select_for_update(self, status, **kwargs):
        """
        Utility method that runs a SELECT FOR UPDATE against all
        Person instances. After the select_for_update, it attempts
        to update the name of the only record, save, and commit.

        This function expects to run in a separate thread.
        """
        status.append('started')
        try:
            # We need to enter transaction management again, as this is done on
            # per-thread basis
            with transaction.atomic():
                person = Person.objects.select_for_update(**kwargs).get()
                person.name = 'Fred'
                person.save()
        except (DatabaseError, Person.DoesNotExist) as e:
            status.append(e)
        finally:
            # This method is run in a separate thread. It uses its own
            # database connection. Close it without waiting for the GC.
            connection.close()
示例#8
0
 def update_birthday_slowly():
     try:
         Person.objects.update_or_create(first_name='John', defaults={'birthday': birthday_sleep})
     finally:
         # Avoid leaking connection for Oracle
         connection.close()