diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2020-02-03 19:07:00 -0800 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-02-06 15:35:23 +0100 |
| commit | 3259983f569151232d8e3b0c3d0de3a858c2b265 (patch) | |
| tree | 4f1f19368fbb9d73ea5245d8fabe5e3097f51926 /django/db | |
| parent | f48f671223a20b161ca819cf7d6298e43b8ba5fe (diff) | |
Fixed #31233 -- Closed database connections and cursors after use.
Diffstat (limited to 'django/db')
| -rw-r--r-- | django/db/backends/mysql/base.py | 6 | ||||
| -rw-r--r-- | django/db/backends/oracle/base.py | 5 | ||||
| -rw-r--r-- | django/db/backends/postgresql/base.py | 8 | ||||
| -rw-r--r-- | django/db/backends/sqlite3/base.py | 5 | ||||
| -rw-r--r-- | django/db/migrations/executor.py | 6 | ||||
| -rw-r--r-- | django/db/migrations/recorder.py | 4 | ||||
| -rw-r--r-- | django/db/models/query.py | 5 | ||||
| -rw-r--r-- | django/db/models/sql/subqueries.py | 5 |
8 files changed, 28 insertions, 16 deletions
diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 527197971f..be00024849 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -269,7 +269,8 @@ class DatabaseWrapper(BaseDatabaseWrapper): forward references. Always return True to indicate constraint checks need to be re-enabled. """ - self.cursor().execute('SET foreign_key_checks=0') + with self.cursor() as cursor: + cursor.execute('SET foreign_key_checks=0') return True def enable_constraint_checking(self): @@ -280,7 +281,8 @@ class DatabaseWrapper(BaseDatabaseWrapper): # nested inside transaction.atomic. self.needs_rollback, needs_rollback = False, self.needs_rollback try: - self.cursor().execute('SET foreign_key_checks=1') + with self.cursor() as cursor: + cursor.execute('SET foreign_key_checks=1') finally: self.needs_rollback = needs_rollback diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index e303ec1f3d..652e323521 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -301,8 +301,9 @@ class DatabaseWrapper(BaseDatabaseWrapper): Check constraints by setting them to immediate. Return them to deferred afterward. """ - self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE') - self.cursor().execute('SET CONSTRAINTS ALL DEFERRED') + with self.cursor() as cursor: + cursor.execute('SET CONSTRAINTS ALL IMMEDIATE') + cursor.execute('SET CONSTRAINTS ALL DEFERRED') def is_usable(self): try: diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 0f45ca93e1..192316d7fb 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -277,13 +277,15 @@ class DatabaseWrapper(BaseDatabaseWrapper): Check constraints by setting them to immediate. Return them to deferred afterward. """ - self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE') - self.cursor().execute('SET CONSTRAINTS ALL DEFERRED') + with self.cursor() as cursor: + cursor.execute('SET CONSTRAINTS ALL IMMEDIATE') + cursor.execute('SET CONSTRAINTS ALL DEFERRED') def is_usable(self): try: # Use a psycopg cursor directly, bypassing Django's utilities. - self.connection.cursor().execute("SELECT 1") + with self.connection.cursor() as cursor: + cursor.execute('SELECT 1') except Database.Error: return False else: diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 642d3d6c5a..7b3f90a2fd 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -296,7 +296,8 @@ class DatabaseWrapper(BaseDatabaseWrapper): return not bool(enabled) def enable_constraint_checking(self): - self.cursor().execute('PRAGMA foreign_keys = ON') + with self.cursor() as cursor: + cursor.execute('PRAGMA foreign_keys = ON') def check_constraints(self, table_names=None): """ @@ -309,7 +310,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): if self.features.supports_pragma_foreign_key_check: with self.cursor() as cursor: if table_names is None: - violations = self.cursor().execute('PRAGMA foreign_key_check').fetchall() + violations = cursor.execute('PRAGMA foreign_key_check').fetchall() else: violations = chain.from_iterable( cursor.execute('PRAGMA foreign_key_check(%s)' % table_name).fetchall() diff --git a/django/db/migrations/executor.py b/django/db/migrations/executor.py index 1f65a7fe99..2765ac28bd 100644 --- a/django/db/migrations/executor.py +++ b/django/db/migrations/executor.py @@ -372,10 +372,8 @@ class MigrationExecutor: else: found_add_field_migration = True continue - columns = self.connection.introspection.get_table_description( - self.connection.cursor(), - table, - ) + with self.connection.cursor() as cursor: + columns = self.connection.introspection.get_table_description(cursor, table) for column in columns: field_column = field.column column_name = column.name diff --git a/django/db/migrations/recorder.py b/django/db/migrations/recorder.py index b8e32a468c..1a37c6b7d0 100644 --- a/django/db/migrations/recorder.py +++ b/django/db/migrations/recorder.py @@ -52,7 +52,9 @@ class MigrationRecorder: def has_table(self): """Return True if the django_migrations table exists.""" - return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) + with self.connection.cursor() as cursor: + tables = self.connection.introspection.table_names(cursor) + return self.Migration._meta.db_table in tables def ensure_schema(self): """Ensure the table exists and has the correct schema.""" diff --git a/django/db/models/query.py b/django/db/models/query.py index 74be5df0ac..a7c16c4bd8 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -747,7 +747,10 @@ class QuerySet: query = self.query.clone() query.__class__ = sql.DeleteQuery cursor = query.get_compiler(using).execute_sql(CURSOR) - return cursor.rowcount if cursor else 0 + if cursor: + with cursor: + return cursor.rowcount + return 0 _raw_delete.alters_data = True def update(self, **kwargs): diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py index af48239cdc..72b6712864 100644 --- a/django/db/models/sql/subqueries.py +++ b/django/db/models/sql/subqueries.py @@ -21,7 +21,10 @@ class DeleteQuery(Query): self.alias_map = {table: self.alias_map[table]} self.where = where cursor = self.get_compiler(using).execute_sql(CURSOR) - return cursor.rowcount if cursor else 0 + if cursor: + with cursor: + return cursor.rowcount + return 0 def delete_batch(self, pk_list, using): """ |
