Commit 3ffeb931 authored by Michael Manfre's avatar Michael Manfre
Browse files

Ensure cursors are closed when no longer needed.

This commit touchs various parts of the code base and test framework. Any
found usage of opening a cursor for the sake of initializing a connection
has been replaced with 'ensure_connection()'.
parent 0837eacc
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ class PostGISCreation(DatabaseCreation):
    @cached_property
    def template_postgis(self):
        template_postgis = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis')
        cursor = self.connection.cursor()
        with self.connection.cursor() as cursor:
            cursor.execute('SELECT 1 FROM pg_database WHERE datname = %s LIMIT 1;', (template_postgis,))
            if cursor.fetchone():
                return template_postgis
@@ -88,7 +88,7 @@ class PostGISCreation(DatabaseCreation):
            # Connect to the test database in order to create the postgis extension
            self.connection.close()
            self.connection.settings_dict["NAME"] = test_database_name
            cursor = self.connection.cursor()
            with self.connection.cursor() as cursor:
                cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
                cursor.connection.commit()

+2 −3
Original line number Diff line number Diff line
@@ -55,9 +55,8 @@ class SpatiaLiteCreation(DatabaseCreation):

        call_command('createcachetable', database=self.connection.alias)

        # Get a cursor (even though we don't need one yet). This has
        # the side effect of initializing the test database.
        self.connection.cursor()
        # Ensure a connection for the side effect of initializing the test database.
        self.connection.ensure_connection()

        return test_database_name

+3 −3
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ def create_default_site(app_config, verbosity=2, interactive=True, db=DEFAULT_DB
        if sequence_sql:
            if verbosity >= 2:
                print("Resetting sequence")
            cursor = connections[db].cursor()
            with connections[db].cursor() as cursor:
                for command in sequence_sql:
                    cursor.execute(command)

+65 −64
Original line number Diff line number Diff line
@@ -59,8 +59,8 @@ class DatabaseCache(BaseDatabaseCache):
        self.validate_key(key)
        db = router.db_for_read(self.cache_model_class)
        table = connections[db].ops.quote_name(self._table)
        cursor = connections[db].cursor()

        with connections[db].cursor() as cursor:
            cursor.execute("SELECT cache_key, value, expires FROM %s "
                           "WHERE cache_key = %%s" % table, [key])
            row = cursor.fetchone()
@@ -75,7 +75,7 @@ class DatabaseCache(BaseDatabaseCache):
            expires = typecast_timestamp(str(expires))
        if expires < now:
            db = router.db_for_write(self.cache_model_class)
            cursor = connections[db].cursor()
            with connections[db].cursor() as cursor:
                cursor.execute("DELETE FROM %s "
                               "WHERE cache_key = %%s" % table, [key])
            return default
@@ -96,8 +96,8 @@ class DatabaseCache(BaseDatabaseCache):
        timeout = self.get_backend_timeout(timeout)
        db = router.db_for_write(self.cache_model_class)
        table = connections[db].ops.quote_name(self._table)
        cursor = connections[db].cursor()

        with connections[db].cursor() as cursor:
            cursor.execute("SELECT COUNT(*) FROM %s" % table)
            num = cursor.fetchone()[0]
            now = timezone.now()
@@ -152,8 +152,8 @@ class DatabaseCache(BaseDatabaseCache):

        db = router.db_for_write(self.cache_model_class)
        table = connections[db].ops.quote_name(self._table)
        cursor = connections[db].cursor()

        with connections[db].cursor() as cursor:
            cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % table, [key])

    def has_key(self, key, version=None):
@@ -162,13 +162,14 @@ class DatabaseCache(BaseDatabaseCache):

        db = router.db_for_read(self.cache_model_class)
        table = connections[db].ops.quote_name(self._table)
        cursor = connections[db].cursor()

        if settings.USE_TZ:
            now = datetime.utcnow()
        else:
            now = datetime.now()
        now = now.replace(microsecond=0)

        with connections[db].cursor() as cursor:
            cursor.execute("SELECT cache_key FROM %s "
                           "WHERE cache_key = %%s and expires > %%s" % table,
                           [key, connections[db].ops.value_to_db_datetime(now)])
@@ -197,7 +198,7 @@ class DatabaseCache(BaseDatabaseCache):
    def clear(self):
        db = router.db_for_write(self.cache_model_class)
        table = connections[db].ops.quote_name(self._table)
        cursor = connections[db].cursor()
        with connections[db].cursor() as cursor:
            cursor.execute('DELETE FROM %s' % table)


+9 −9
Original line number Diff line number Diff line
@@ -72,7 +72,7 @@ class Command(BaseCommand):
            full_statement.append('    %s%s' % (line, ',' if i < len(table_output) - 1 else ''))
        full_statement.append(');')
        with transaction.commit_on_success_unless_managed():
            curs = connection.cursor()
            with connection.cursor() as curs:
                try:
                    curs.execute("\n".join(full_statement))
                except DatabaseError as e:
Loading