Do database transaction commands (commit/rollback) also work for DocType transactions?

Apparently this is possible for Database work:

try:
    frappe.db.begin()
    -----> DATABASE OPERATIONS <-----
except Exception as e:
    frappe.db.rollback()
else:
    frappe.db.commit()
finally:
    frappe.destroy()

By the way, the documentation for frappe.db mentions neither frappe.db.begin() nor frappe.destroy(). Is that an omission or are they both deprecated?

Questions:

Is the above mentioned try ... except ... finally expression also possible with DocType transactions? If not, how does one ensure atomicity?

For example:

try:
    frappe.db.begin()
    tank.db_set('fill_level', 80, commit=True)
    gauge.db_set('fill_level', 80, commit=True)
    stock_entry.save()    
except Exception as e:
    frappe.db.rollback()
else:
    frappe.db.commit()
finally:
    frappe.destroy()
2 Likes