summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2014-04-09 22:41:33 +0200
committerAymeric Augustin <aymeric.augustin@m4x.org>2014-04-09 22:54:39 +0200
commit6fa7d7c594597ab7072ffa740da5b6f4a5a6cd26 (patch)
tree14e5d4946737e82f4169a13c8d35b26e855b6a07
parent32529e6432dfca3ace93d129cdb22fcebc3a2730 (diff)
[1.6.x] Fixed #21553 -- Ensured unusable database connections get closed.
Backport of 5f2f47f from master
-rw-r--r--django/db/backends/__init__.py7
-rw-r--r--django/db/backends/mysql/base.py2
-rw-r--r--django/db/backends/oracle/base.py2
-rw-r--r--django/db/backends/postgresql_psycopg2/base.py2
-rw-r--r--tests/backends/tests.py31
5 files changed, 40 insertions, 4 deletions
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index 55ed1f0a16..480b81be7f 100644
--- a/django/db/backends/__init__.py
+++ b/django/db/backends/__init__.py
@@ -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):
"""
diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
index d760418aaf..87401f2da9 100644
--- a/django/db/backends/mysql/base.py
+++ b/django/db/backends/mysql/base.py
@@ -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
diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py
index 46f89ffd2f..7610045f67 100644
--- a/django/db/backends/oracle/base.py
+++ b/django/db/backends/oracle/base.py
@@ -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
diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
index ec2b71c3a1..23da101d01 100644
--- a/django/db/backends/postgresql_psycopg2/base.py
+++ b/django/db/backends/postgresql_psycopg2/base.py
@@ -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
diff --git a/tests/backends/tests.py b/tests/backends/tests.py
index 52897e4732..9f8d14279e 100644
--- a/tests/backends/tests.py
+++ b/tests/backends/tests.py
@@ -612,6 +612,37 @@ class BackendTestCase(TestCase):
cursor.execute(query)
+class IsUsableTests(TransactionTestCase):
+ # Avoid using a regular TestCase because Django really dislikes closing
+ # the database connection inside a transaction at this point (#21202).
+
+ available_apps = []
+
+ # Unfortunately with sqlite3 the in-memory test database cannot be closed.
+ @skipUnlessDBFeature('test_db_allows_multiple_connections')
+ def test_is_usable_after_database_disconnects(self):
+ """
+ Test that is_usable() doesn't crash when the database disconnects.
+
+ Regression for #21553.
+ """
+ # Open a connection to the database.
+ with connection.cursor():
+ pass
+ # Emulate a connection close by the database.
+ connection._close()
+ # Even then is_usable() should not raise an exception.
+ try:
+ self.assertFalse(connection.is_usable())
+ finally:
+ # Clean up the mess created by connection._close(). Since the
+ # connection is already closed, this crashes on some backends.
+ try:
+ connection.close()
+ except Exception:
+ pass
+
+
# We don't make these tests conditional because that means we would need to
# check and differentiate between:
# * MySQL+InnoDB, MySQL+MYISAM (something we currently can't do).