﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30448	close_if_unusable_or_obsolete should skip connections in atomic block for autocommit check	Daniel Hahler	Samriddha Kumar Tripathi	"Via https://github.com/django/channels/issues/1091#issuecomment-489488831 I have noticed that `close_if_unusable_or_obsolete` will close the DB connection used in tests, which has entered an atomic block (via `TestCase`).

channel's `DatabaseSyncToAsync` calls `close_if_unusable_or_obsolete` here.

The following patch might make sense:

{{{
diff --git c/django/db/backends/base/base.py i/django/db/backends/base/base.py
index 9fa03cc0ee..f9ca0f8464 100644
--- c/django/db/backends/base/base.py
+++ i/django/db/backends/base/base.py
@@ -497,7 +497,10 @@ def close_if_unusable_or_obsolete(self):
         if self.connection is not None:
             # If the application didn't restore the original autocommit setting,
             # don't take chances, drop the connection.
-            if self.get_autocommit() != self.settings_dict['AUTOCOMMIT']:
+            if (
+                    not self.in_atomic_block and
+                    self.get_autocommit() != self.settings_dict[""AUTOCOMMIT""]
+            ):
                 self.close()
                 return
 
}}}"	Bug	assigned	Database layer (models, ORM)	2.2	Normal			Jon Janzen Evstifeev Roman Anders Kaseorg	Accepted	1	0	0	0	0	0
