leave_transaction_management removes threads from state, but not dirty. This will cause a leak as threads are destroyed, and causes the set_clean and set_dirty sanity check ("This code isn't under transaction management") to not always trigger. It may also cause problems if created threads get the same thread identifier as a previous thread. Attached patch cleans up the transaction state when exiting transaction management; obviously, TLS would be a cleaner and more reliable solution.
This patch causes a failure in regressiontests.cache.tests.DBCacheTests:
======================================================================
ERROR: test_add (regressiontests.cache.tests.DBCacheTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/glenn/django/tests/regressiontests/cache/tests.py", line 142, in test_add
result = self.cache.add("addkey1", "newvalue")
File "/home/glenn/django/django/core/cache/backends/db.py", line 45, in add
return self._base_set('add', key, value, timeout)
File "/home/glenn/django/django/core/cache/backends/db.py", line 68, in _base_set
transaction.rollback()
File "/home/glenn/django/django/db/transaction.py", line 180, in rollback
set_clean()
File "/home/glenn/django/django/db/transaction.py", line 114, in set_clean
raise TransactionManagementError("This code isn't under transaction management")
TransactionManagementError: This code isn't under transaction management
I don't know what _base_set should be doing, but it seems like this error is intended to be triggered in this situation.
cleanup-transaction-state-on-exit.diff fails to apply cleanly on to trunk