From 7dfe4ea2f6c62ffb662085cb4d62a13b8e902ce1 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 23 Sep 2015 00:04:27 +0200 Subject: Replace close_connection() for newer django versions django.db.close_connection() is deprecated in django1.7 (and removed in 1.8). The new django.db.close_old_connections() does not seem to work like the old one (see http://stackoverflow.com/a/32614137), so we replace it with an own implementation. --- src/lib/Bcfg2/Reporting/Storage/DjangoORM.py | 8 ++++++-- src/lib/Bcfg2/Server/Core.py | 9 +++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py b/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py index c9aa169bf..7acd9e293 100644 --- a/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py +++ b/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py @@ -11,11 +11,11 @@ import Bcfg2.DBSettings from Bcfg2.Compat import md5 from Bcfg2.Reporting.Storage.base import StorageBase, StorageError from Bcfg2.Server.Plugin.exceptions import PluginExecutionError +import django from django.core import management from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned from django.db.models import FieldDoesNotExist from django.core.cache import cache -from django import db #Used by GetCurrentEntry import difflib @@ -383,8 +383,12 @@ class DjangoORM(StorageBase): finally: self.logger.debug("%s: Closing database connection" % self.__class__.__name__) - db.close_connection() + if django.VERSION[0] == 1 and django.VERSION[1] >= 7: + for connection in django.db.connections.all(): + connection.close() + else: + django.db.close_connection() def validate(self): """Validate backend storage. Should be called once when loaded""" diff --git a/src/lib/Bcfg2/Server/Core.py b/src/lib/Bcfg2/Server/Core.py index 4592688e7..87bbe7e1b 100644 --- a/src/lib/Bcfg2/Server/Core.py +++ b/src/lib/Bcfg2/Server/Core.py @@ -27,6 +27,7 @@ from Bcfg2.Server.Statistics import track_statistics try: from django.core.exceptions import ImproperlyConfigured + import django import django.conf HAS_DJANGO = True except ImportError: @@ -83,10 +84,14 @@ def close_db_connection(func): """ The decorated function """ rv = func(self, *args, **kwargs) if self._database_available: # pylint: disable=W0212 - from django import db self.logger.debug("%s: Closing database connection" % threading.current_thread().getName()) - db.close_connection() + + if django.VERSION[0] == 1 and django.VERSION[1] >= 7: + for connection in django.db.connections.all(): + connection.close() + else: + django.db.close_connection() return rv return inner -- cgit v1.2.3-1-g7c22