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:

  1. >>> db.commit()

To check it let’s insert a new record:

  1. >>> db.person.insert(name="Bob")
  2. 2

and roll back, i.e., ignore all operations since the last commit:

  1. >>> db.rollback()

If you now insert again, the counter will again be set to 2, since the previous insert was rolled back.

  1. >>> db.person.insert(name="Bob")
  2. 2

Code in models, views and controllers is enclosed in web2py code that looks like this (pseudo code) :

  1. try:
  2. execute models, controller function and view
  3. except:
  4. rollback all connections
  5. log the traceback
  6. send a ticket to the visitor
  7. else:
  8. commit all connections
  9. 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().