Re: modify cycles
Roger Binns <[email protected]>
| Newsgroups | gmane.comp.python.db.pysqlite.user |
|---|---|
| Message-ID | <[email protected]> |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Eric S. Johansson wrote:
> what kind of exceptions?
Any of the SQLite methods can have exceptions. And the method you call
(fcn) is way more likely to have them as well. You need to code the
transaction method such that no matter what happens inside the isolation
level is restored to what it was at entry and that a transaction is not
in place.
You also don't want to swallow exceptions which hides what is going on.
For example is fcn did 1/0, the ZeroDivision exception would be
swallowed by the code you list. You can do the fixups and then do
'raise' which will cause the exception to continue being raised. Also
see the 'finally' clause for try.
> def transaction(connection, fcn, args**):
> success = False
> old_isolation = connection.isolation_level
> connection.isolation_level=None
> c=connection.cursor()
That could raise an exception
> c.execute("BEGIN")
So can that (eg there is already a transaction underway as SQLite
doesn't support nested exceptions).
> while(True):
> try:
> fcn(c,args)
That could have any manner of exceptions.
> c.execute("END")
> connection.commit()
Those both do the same thing.
> success = True
> break
> except (..retryable exceptions...), error:
> continue
> except (..stbu exceptions...), error:
> c.execute("ROLLBACK")
> break
That swallows the exception that was raised.
> connection.isolation_level = old_isolation
> return success
Here is one example of doing it all. Look at line 243.
http://bitpim.svn.sourceforge.net/viewvc/bitpim/trunk/bitpim/src/database.py?view=markup
It is used to wrap a method making the method exclusive and does
counting to emulate nested transactions as well as other gunk in there
won't care about.
Roger
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFF6JvwmOOfHg372QQRAvS0AKCgfpL80BxFvb0oygXL59irzqQDQwCeMzyV
ZtNuNcEGQklDP/gQpuyI508=
=CARQ
-----END PGP SIGNATURE-----