raise if isolation_level execution option is used when a Transaction is present; warning in 0.9

Issue #3296 resolved
Mike Bayer repo owner created an issue

isolation level settings typically do not work until the next transaction, and at least in the case of Psycopg2 are destructive. For this reason, the MySQL dialect already implements a COMMIT, and psycopg2 actually resets the transaction when you change the level:

>>> import psycopg2
>>> conn = psycopg2.connect(user='scott', dbname='test', password='tiger', host='localhost')
>>> cursor = conn.cursor()
>>> cursor.execute("create table foo (id integer)")
>>> cursor.execute("insert into foo (id) values (1)")
>>> cursor.close()
>>> from psycopg2 import extensions
>>> conn.set_isolation_level(extensions.ISOLATION_LEVEL_SERIALIZABLE)
>>> cursor = conn.cursor()
>>> cursor.execute("select * from foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
psycopg2.ProgrammingError: relation "foo" does not exist
LINE 1: select * from foo
                      ^

huge warning is needed and definitely an exception in 1.0.

Comments (3)

  1. Mike Bayer reporter

    this must include some approach to accommodate the user that is doing this:

    session.connection().execution_options(isolation_level=...)
    

    for that, we either need to get the warning to not emit if no SQL has yet been emitted, or provide some new pattern for the above.

  2. Mike Bayer reporter
    • A warning is emitted if the isolation_level parameter is used with :meth:.Connection.execution_options when a :class:.Transaction is in play; DBAPIs and/or SQLAlchemy dialects such as psycopg2, MySQLdb may implicitly rollback or commit the transaction, or not change the setting til next transaction, so this is never safe.
    • Added new parameter :paramref:.Session.connection.execution_options which may be used to set up execution options on a :class:.Connection when it is first checked out, before the transaction has begun. This is used to set up options such as isolation level on the connection before the transaction starts.
    • added new documentation section detailing best practices for setting transaction isolation with sessions. fixes #3296

    → <<cset 9cbe235810b7>>

  3. Mike Bayer reporter
    • A warning is emitted if the isolation_level parameter is used with :meth:.Connection.execution_options when a :class:.Transaction is in play; DBAPIs and/or SQLAlchemy dialects such as psycopg2, MySQLdb may implicitly rollback or commit the transaction, or not change the setting til next transaction, so this is never safe.
    • Added new parameter :paramref:.Session.connection.execution_options which may be used to set up execution options on a :class:.Connection when it is first checked out, before the transaction has begun. This is used to set up options such as isolation level on the connection before the transaction starts.
    • added new documentation section detailing best practices for setting transaction isolation with sessions. fixes #3296

    (cherry picked from commit 9cbe235810b7c0c24d2556b4bb581b0207812e2d)

    Conflicts: test/orm/test_bind.py

    → <<cset 280497cc5bda>>

  4. Log in to comment