How do I do transaction in web2py?

146 views
Skip to first unread message

Xiaojian Huang

unread,
May 11, 2014, 8:34:00 PM5/11/14
to web...@googlegroups.com
I want to insert records into two tables A  and B. I will insert into A first then B.

and I like to rollback A when inserting into B failed. Is there any way to do this?

Thanks!

Dragan Matic

unread,
May 12, 2014, 4:28:19 AM5/12/14
to web...@googlegroups.com
All database operations in your controllers which are executed through DAL are executed in a transaction.
So every time you do a database operation in your controller it starts a new transaction which is automatically commited if there were no errors. If it encounters any error then the transaction is rolled back.

If you still want to control your transactions (e.g. if you wish to have more transaction in a single controller call) you can do it through db.commit() and db.rollback().

Take a look at this:
http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer?search=transaction#commit-and-rollback
Reply all
Reply to author
Forward
0 new messages