﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13870	Document transaction/connection management outside the web server context	Patryk Zawadzki	Aymeric Augustin	"First some background info:

 1. PostgreSQL provides several transaction isolation levels.
 2. Django's psycopg backend defaults to ghost-read-preventing isolation.
 3. psycopg isolation modes automatically create the isolating transaction whenever you execute your first SQL statement.
 4. psycopg expects you to tell it when you're done with your work by either running `connection.commit()` or `connection.rollback()`
 5. When you terminate the transaction, psycopg will wait for the next SQL statement and start a new isolating transaction.

The problem is: Django breaks assumption 4 and never commits or rollbacks the isolating transaction. Note I'm not talking about the transactions explicitly started by django, I'm talking about the transaction implicitly started by requesting a non-zero isolation level. This has the nasty side-effect of leaving the connection permanently in-transaction.

Try it yourselves: start a django server, make it use the DB and then try to - say - `VACUUM FULL` the table it selected from. Or try to `ALTER` the table (run a south migration). You will end up with a hanging connection, waiting for Django to leave the transaction block.

The attached patch makes it work for psycopg2 by introducing a new method in the backend class. It needs a call to `leave_transaction_management()` so it works best with the `TransactionMiddleware`.

Now for a real solution:

I think it would be better to introduce some sort of `enter_isolation_block()` / `leave_isolation_block()` API. ""Enter"" would call `set_isolation_level(1)` (currently done unconditionally), ""leave"" would terminate the isolation transaction. Then introduce a default middleware that wraps all the views in these calls. It just doesn't seem intuitive to mix the transactions implicitly created by isolation API with the explicit transaction management API."	New feature	closed	Documentation	dev	Normal	fixed		timograham@…	Accepted	1	0	0	1	0	0
