﻿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
21453	Set autocommit mode before calling init_connection_state	Aymeric Augustin	Aymeric Augustin	"I just helped an user on IRC who attempted a fairly reasonable database backend customization:
{{{
from django.db.backends.postgresql_psycopg2 import base
     
class DatabaseWrapper(base.DatabaseWrapper):
    def init_connection_state(self):
        super(DatabaseWrapper, self).init_connection_state()
        self.connection.cursor().execute('SET search_path TO something')
}}}

Unfortunately that fails with an ugly `ProgrammingError: autocommit cannot be used inside a transaction` and the traceback is very confusing.

The root cause of this problem is that the code above starts a transaction, and when Django later tries to switch to autocommit, PostgreSQL complains.

Would it make sense to switch to autocommit before calling `init_connection_state`? This is just a matter of swapping two lines in `BaseDatabaseWrapper.connect`, and reviewing carefully the code in `init_connection_state` to ensure it still works as expected. (Hint: it doesn't, see #21452.)

Currently `init_connection_state` executes in PEP-249 mode (= implicit transactions = no autocommit), while everything else executes in autocommit mode ''(unless you set AUTOCOMMIT to False in the database settings, but I'm really mentioning that for the sake of completeness, I don't expect anyone to do this)''. That's quite surprising and probably a good reason for making this change. Of course, there may be even better reasons for NOT making this change that I haven't thought of.
"	Cleanup/optimization	closed	Database layer (models, ORM)	1.6	Normal	fixed		w2lkm2n@…	Accepted	0	0	0	0	0	0
