commit
and rollback
The insert, truncate, delete, and update operations aren’t actually committed until web2py issues the commit command. The create and drop operations may be executed immediately, depending on the database engine. Calls to web2py actions are automatically wrapped in transactions. If you executed commands via the shell, you are required to manually commit:
>>> db.commit()
To check it let’s insert a new record:
>>> db.person.insert(name="Bob")
2
and roll back, i.e., ignore all operations since the last commit:
>>> db.rollback()
If you now insert again, the counter will again be set to 2, since the previous insert was rolled back.
>>> db.person.insert(name="Bob")
2
Code in models, views and controllers is enclosed in web2py code that looks like this (pseudo code) :
try:
execute models, controller function and view
except:
rollback all connections
log the traceback
send a ticket to the visitor
else:
commit all connections
save cookies, sessions and return the page
So in models, views and controllers there is no need to ever call commit
or rollback
explicitly in web2py unless you need more granular control. However, in modules you will need to use commit()
.