diff --git a/cachalot/monkey_patch.py b/cachalot/monkey_patch.py index 3dafb1d..3a88d75 100644 --- a/cachalot/monkey_patch.py +++ b/cachalot/monkey_patch.py @@ -240,13 +240,15 @@ def _patch_atomic(): def patch_exit(original): @wraps(original) def inner(self, exc_type, exc_value, traceback): - nested_caches = NESTED_CACHES[cachalot_settings.CACHALOT_CACHE] - atomic_cache = nested_caches.pop() - if exc_type is None and not connection.needs_rollback: - atomic_cache.commit() + needs_rollback = connection.needs_rollback original(self, exc_type, exc_value, traceback) + nested_caches = NESTED_CACHES[cachalot_settings.CACHALOT_CACHE] + atomic_cache = nested_caches.pop() + if exc_type is None and not needs_rollback: + atomic_cache.commit() + inner.original = original return inner