summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHonza Král <honza.kral@gmail.com>2011-08-29 15:55:06 +0000
committerHonza Král <honza.kral@gmail.com>2011-08-29 15:55:06 +0000
commit7c657b241657de6b7551dd3f064abe08478719c7 (patch)
treeed864d1ecb2449467994897aaa2321456f057678
parent6dc48a735c2d98c4d1e5a56cb99d62a3631c3bcd (diff)
Fixed #15802 -- pyscopg2 sometimes fail to close the connection when it's already closed by the server, Thanks Rick van Hattem
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16708 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/db/backends/postgresql_psycopg2/base.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
index 37aa072f6b..aaf6262038 100644
--- a/django/db/backends/postgresql_psycopg2/base.py
+++ b/django/db/backends/postgresql_psycopg2/base.py
@@ -14,6 +14,7 @@ from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
from django.db.backends.postgresql_psycopg2.version import get_version
from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection
from django.utils.safestring import SafeUnicode, SafeString
+from django.utils.log import getLogger
try:
import psycopg2 as Database
@@ -29,6 +30,8 @@ psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_adapter(SafeString, psycopg2.extensions.QuotedString)
psycopg2.extensions.register_adapter(SafeUnicode, psycopg2.extensions.QuotedString)
+logger = getLogger('django.db.backends')
+
class CursorWrapper(object):
"""
A thin wrapper around psycopg2's normal cursor class so that we can catch
@@ -114,6 +117,24 @@ class DatabaseWrapper(BaseDatabaseWrapper):
self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
self.cursor().execute('SET CONSTRAINTS ALL DEFERRED')
+ def close(self):
+ if self.connection is None:
+ return
+
+ try:
+ self.connection.close()
+ self.connection = None
+ except psycopg2.Error:
+ # In some cases (database restart, network connection lost etc...)
+ # the connection to the database is lost without giving Django a
+ # notification. If we don't set self.connection to None, the error
+ # will occur a every request.
+ self.connection = None
+ logger.warning('psycopg2 error while closing the connection.',
+ exc_info=sys.exc_info()
+ )
+ raise
+
def _get_pg_version(self):
if self._pg_version is None:
self._pg_version = get_version(self.connection)