Re: [pysqlite] [APSW] "SQLError: cannot rollback - no transaction is active"

Roger Binns <[email protected]> Fri, 28 Nov 2008 19:53:58 -0800
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Fred wrote:
> Out of curiosity, why does APSW raise this error in this try/except block?
[..]
> try:
> 	cursor.execute("COMMIT")
> 	time.sleep(1)
> except:
> 	print "Failed %s" % sql
> 	File "src/cursor.c", line 230, in resetcursor apsw.SQLError: 
> SQLError: cannot rollback - no transaction is active
> 	cursor.execute("ROLLBACK")
> 	connection.close(True)
> 	sys.exit()
> =========
> 
> Since the COMMIT failed, I assumed the ROLLBACK would be ran in the 
> except section that follows.

Who says the commit failed?  The exception is from the rollback and that
second exception would mask any exception in the try block.  An example
of how you would see the above is if you hadn't imported the time module.

In general it is a really bad idea to do a blanket except statement like
you have since it catches all exceptions.  In this case it is also
redundant as you want to rollback and exit.  When a connection is
shutdown with a transaction in progress it is rolled back.  (This would
happen as a normal part of the Python interpreter shutdown).  Even if
you forced an abrupt process termination (eg os._exit) then the next
SQLite open will rollback the transaction.

Roger
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkkwvNIACgkQmOOfHg372QSgvgCgmVIhZ7NkX6IeGb+z9gnNNiap
K10AoMK8UrxZESsauVV5EuuY35eYAAHR
=asWM
-----END PGP SIGNATURE-----