Author nagylzs
Recipients georg.brandl, nagylzs
Date 2010-03-15.09:01:41
SpamBayes Score 0.00211249
Marked as misclassified No
Message-id <1268643705.49.0.319902511171.issue8145@psf.upfronthosting.co.za>
In-reply-to
Content
Clarify what isolation_level does, and how to use it, and why connections do not commit/rollback in some cases.

Details here:

http://mail.python.org/pipermail/python-list/2010-March/1239374.html

I'll paste code for ctx_manager_2.py here. This is a new file, I could not include it as a diff:

import sqlite3

class MyConn(sqlite3.Connection):
    def __enter__(self):
        self.execute("BEGIN")
        return self
    def __exit__(self,exc_type,exc_info,traceback):
        if exc_type is None:
            self.execute("COMMIT")
        else:
            self.execute("ROLLBACK")


conn = sqlite3.connect(':memory:',factory=MyConn)
conn.isolation_level = None
with conn:
    conn.execute("create table a ( i integer ) ")
    conn.execute("insert into a values (1)")
try:
    with conn:
        conn.execute("insert into a values (2)")
        conn.execute("savepoint sp1")
        conn.execute("insert into a values (3)")
        conn.execute("rollback to sp1")
        conn.execute("insert into a values (4)")
        print "Before rollback: 1,2,4"
        for row in conn.execute("select * from a"):
            print row[0] # prints 1,2,4
        raise Exception
except:
    pass
print "After rollback: 1"
for row in conn.execute("select * from a"):
    print row[0] # prints 1
History
Date User Action Args
2010-03-15 09:01:46nagylzssetrecipients: + nagylzs, georg.brandl
2010-03-15 09:01:45nagylzssetmessageid: <1268643705.49.0.319902511171.issue8145@psf.upfronthosting.co.za>
2010-03-15 09:01:43nagylzslinkissue8145 messages
2010-03-15 09:01:42nagylzscreate