Fixed #25329 -- Prevented _nodb_connection from being left open
This commit is contained in:
parent
d3c92afe42
commit
b2f6e421a3
|
@ -569,10 +569,10 @@ class BaseDatabaseWrapper(object):
|
||||||
if must_close:
|
if must_close:
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
@cached_property
|
@property
|
||||||
def _nodb_connection(self):
|
def _nodb_connection(self):
|
||||||
"""
|
"""
|
||||||
Alternative connection to be used when there is no need to access
|
Return an alternative connection to be used when there is no need to access
|
||||||
the main database, specifically for test db creation/deletion.
|
the main database, specifically for test db creation/deletion.
|
||||||
This also prevents the production database from being exposed to
|
This also prevents the production database from being exposed to
|
||||||
potential child threads while (or after) the test database is destroyed.
|
potential child threads while (or after) the test database is destroyed.
|
||||||
|
|
|
@ -232,7 +232,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@cached_property
|
@property
|
||||||
def _nodb_connection(self):
|
def _nodb_connection(self):
|
||||||
nodb_connection = super(DatabaseWrapper, self)._nodb_connection
|
nodb_connection = super(DatabaseWrapper, self)._nodb_connection
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -173,12 +173,10 @@ class PostgreSQLTests(TestCase):
|
||||||
self.assertIsNone(nodb_conn.settings_dict['NAME'])
|
self.assertIsNone(nodb_conn.settings_dict['NAME'])
|
||||||
|
|
||||||
# Now assume the 'postgres' db isn't available
|
# Now assume the 'postgres' db isn't available
|
||||||
del connection._nodb_connection
|
|
||||||
with warnings.catch_warnings(record=True) as w:
|
with warnings.catch_warnings(record=True) as w:
|
||||||
with mock.patch('django.db.backends.base.base.BaseDatabaseWrapper.connect',
|
with mock.patch('django.db.backends.base.base.BaseDatabaseWrapper.connect',
|
||||||
side_effect=mocked_connect, autospec=True):
|
side_effect=mocked_connect, autospec=True):
|
||||||
nodb_conn = connection._nodb_connection
|
nodb_conn = connection._nodb_connection
|
||||||
del connection._nodb_connection
|
|
||||||
self.assertIsNotNone(nodb_conn.settings_dict['NAME'])
|
self.assertIsNotNone(nodb_conn.settings_dict['NAME'])
|
||||||
self.assertEqual(nodb_conn.settings_dict['NAME'], connection.settings_dict['NAME'])
|
self.assertEqual(nodb_conn.settings_dict['NAME'], connection.settings_dict['NAME'])
|
||||||
# Check a RuntimeWarning has been emitted
|
# Check a RuntimeWarning has been emitted
|
||||||
|
|
Loading…
Reference in New Issue