jmoiron/johnny-cache — pull request #34 by Ashald (2013-10-04 08:43:41+00:00)
Title: Fixed bug that caused KeyError in _commit_all_savepoints

Fixed bug that caused KeyError in johnny.transaction.TransactionManager._commit_all_savepoints - issues #26 and #30 on Github ([#69](https://bitbucket.org/jmoiron/johnny-cache/issue/69/keyerror-on-some-transactions) on Bitbucket)

This patch is fixing cause of the problem, not hiding it as pull requests #26 and #30.

To reproduce cause of the problem you should:
1) Enable _QueryCacheMiddleware_
2) Work in unmanaged transaction state (it means autocommit mode, i.e. without _django.middleware.transaction.TransactionMiddleware_ and not within _transaction.enter_transaction_management()_ block)
3) Create a savepoint and then try to save some object (it is done by _django.db.models.query.QuerySet#get_or_create_ or by _django.contrib.sessions.backends.db.SessionStore#save_)

The essence of the problem is the fact that Django ORM widely uses a _django/db/transaction.py#commit_unless_managed_ (which is __unpatched__ by Johny Cache) function instead of _django/db/transaction.py#commit_ (which is __patched__ by Johny Cache). So, when we working in unmanaged transaction state Johny Cache will still register savepoints upon creation but will be unable to handle commits and rollbacks that wast performed by 'unless_managed' functions. Since Django is using dumb algorithm for savepoint names generation (they reuse same names within thread once they were commited) it will lead to a situation when Johny Cache will register several savepoints with same name within one transaction and fail when it will try to commit or rollback them.

Proposed solution adds patching to _django/db/transaction.py#commit_unless_managed_ and _django/db/transaction.py#rollback_unless_managed_ functions in the same way as it was done for _django/db/transaction.py#commit_ and _django/db/transaction.py#rollback_ functions with one difference - additional check whether current transaction is unmanaged.