Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug that caused KeyError in _commit_all_savepoints #34

Merged
merged 3 commits into from
Oct 4, 2013
Merged

Fixed bug that caused KeyError in _commit_all_savepoints #34

merged 3 commits into from
Oct 4, 2013

Conversation

ashald
Copy link
Contributor

@ashald ashald commented Oct 4, 2013

Fixed bug that caused KeyError in johnny.transaction.TransactionManager._commit_all_savepoints - issues #26 and #30 on Github (#69 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.

ashald added 3 commits October 4, 2013 11:31
…er._commit_savepoint - issue #30 on Github (#69 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_unless_managed' (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.
jmoiron added a commit that referenced this pull request Oct 4, 2013
Fixed bug that caused KeyError in _commit_all_savepoints
@jmoiron jmoiron merged commit 5bd9028 into jmoiron:master Oct 4, 2013
@jmoiron
Copy link
Owner

jmoiron commented Oct 4, 2013

If you could contribute a test case that checks for this behavior that would be amazing, but otherwise jself or I will get around to it when we have the time. Thanks for the PR!

@ashald
Copy link
Contributor Author

ashald commented Oct 4, 2013

Ok, I'll add a tests for this fix and for my previous fix in few days and create a new pull request.

@ashald ashald deleted the patch-2 branch October 5, 2013 07:57
@ashald
Copy link
Contributor Author

ashald commented Oct 9, 2013

I have created PR #35 with tests for this PR and PR #33.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants