|
6 | 6 | HAS_INC_THREAD_SHARING = django.VERSION >= (2, 2) |
7 | 7 |
|
8 | 8 |
|
| 9 | +def _close_old_connections(): |
| 10 | + """Like django.db.close_old_connections, but skipping in_atomic_block. |
| 11 | +
|
| 12 | + Ref: https://code.djangoproject.com/ticket/30448 |
| 13 | + Ref: https://github.com/django/django/pull/11769 |
| 14 | + """ |
| 15 | + for conn in connections.all(): |
| 16 | + if not conn.in_atomic_block: |
| 17 | + conn.close_if_unusable_or_obsolete() |
| 18 | + |
| 19 | + |
9 | 20 | class DatabaseSyncToAsync(SyncToAsync): |
10 | 21 | """ |
11 | 22 | SyncToAsync version that cleans up old database connections. |
@@ -34,22 +45,13 @@ def _inherit_main_thread_connections(self): |
34 | 45 | connections[name].allow_thread_sharing = True |
35 | 46 | return restore_allow_thread_sharing |
36 | 47 |
|
37 | | - def _close_old_connections(self): |
38 | | - """Like django.db.close_old_connections, but skipping in_atomic_block. |
39 | | -
|
40 | | - Ref: https://github.com/django/django/pull/11769 |
41 | | - """ |
42 | | - for conn in connections.all(): |
43 | | - if not conn.in_atomic_block: |
44 | | - conn.close_if_unusable_or_obsolete() |
45 | | - |
46 | 48 | def thread_handler(self, loop, *args, **kwargs): |
47 | 49 | restore_allow_thread_sharing = self._inherit_main_thread_connections() |
48 | | - self._close_old_connections() |
| 50 | + _close_old_connections() |
49 | 51 | try: |
50 | 52 | return super().thread_handler(loop, *args, **kwargs) |
51 | 53 | finally: |
52 | | - self._close_old_connections() |
| 54 | + _close_old_connections() |
53 | 55 | for name, saved_sharing in restore_allow_thread_sharing.items(): |
54 | 56 | connections[name].allow_thread_sharing = saved_sharing |
55 | 57 |
|
|
0 commit comments