rosuav at gmail.com
Thu Apr 9 12:36:51 CEST 2015
On Thu, Apr 9, 2015 at 7:31 PM, Palpandi <palpandi111 at gmail.com> wrote:
> Hi all,
> Is there any way to roll back or undo changes which are all done before exception occurs.
In Python itself? Not directly; there are no facilities for undoing
Python code. But if you're serious about integrity, you probably want
to be (or already are) using a database, eg PostgreSQL, and storing
data in there. In that case, you can follow a fairly straight-forward
model, and one that some database connection modules even make easy
# do stuff that might affect the database
# and might throw an exception
As you leave the 'with' block, Python will tell the database
connection "hey, we got out without a problem", or "hey, we're leaving
here because of an exception". In the first case, the database will
commit, so everything you've done really happens. In the second, it'll
roll back, which will undo everything perfectly.
But this applies only to the database. For this to work, you have to
organize your code around that; make sure that everything that matters
is in the database, and local objects can't ever need to be "undone".
More information about the Python-list