1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

[1.6.x] Fixed #21553 -- Ensured unusable database connections get closed.

Backport of 5f2f47f from master
This commit is contained in:
Aymeric Augustin
2014-04-09 22:41:33 +02:00
parent 32529e6432
commit 6fa7d7c594
5 changed files with 40 additions and 4 deletions

View File

@@ -442,9 +442,14 @@ class BaseDatabaseWrapper(object):
def is_usable(self):
"""
Tests if the database connection is usable.
This function may assume that self.connection is not None.
Actual implementations should take care not to raise exceptions
as that may prevent Django from recycling unusable connections.
"""
raise NotImplementedError
raise NotImplementedError(
"subclasses of BaseDatabaseWrapper may require an is_usable() method")
def close_if_unusable_or_obsolete(self):
"""

View File

@@ -517,7 +517,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
def is_usable(self):
try:
self.connection.ping()
except DatabaseError:
except Database.Error:
return False
else:
return True

View File

@@ -642,7 +642,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
else:
# Use a cx_Oracle cursor directly, bypassing Django's utilities.
self.connection.cursor().execute("SELECT 1 FROM DUAL")
except DatabaseError:
except Database.Error:
return False
else:
return True

View File

@@ -190,7 +190,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
try:
# Use a psycopg cursor directly, bypassing Django's utilities.
self.connection.cursor().execute("SELECT 1")
except DatabaseError:
except Database.Error:
return False
else:
return True