Improve connection._dirty flag handling
|Reported by:||Anssi Kääriäinen||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The connection._dirty flag currently represents a mix of things:
- It is sometimes used for tracking if transaction management is enabled (enabled when self._dirty is not None). This doesn't work correctly, as the _dirty flag is never reset to None. And, three valued flag isn't a good idea anyways.
- It is used to track if the connection needs commit, but only inside transaction management. When outside tx management it will not be set by Django.
Patch available here: https://github.com/akaariai/django/commit/cd31b74c01eca1744bc07f1b462ce73a78666fa6. I believe the commit resolves a bunch of subtle bugs, but it likely also causes a bunch of subtle backwards incompatibilities. One such change is that .commit() and .rollback() can now be called always, not only inside transaction management.
I tried the patch and it passes all tests on sqlite and postgresql (with autocommit off, autocommit on I had time to run just backends transactions transactions_regress and these pass).
With postgresql I also ran the tests with transaction state tracking enabled (the commented out code in psycopg2/base.py). There is one false positive (Django believes the transaction to be in transaction, in reality it isn't) caused by test_cursor_executemany_with_empty_params_list in backends tests. executemany with empty params does nothing, but the dirty flag handling doesn't spot this. This is likely a non issue (or, if this is a issue, it is a bug in current Django, too). There are no false negatives (Django doesn't believe the connection to be in tx, but it is).
The patch also contains a bunch of fixes to tests doing various weird things with transaction management.