How to save changes to two tables in same transaction on the serverΒΆ
Below are two examples.
In the first example each apply method gets its own connection from connection pool and commits it after saving changes to the database.
In the second example the connection is received from connection pool and passed to each apply method so changes are committed at the end.
import datetime
def change_invoice_date(item, invoice_id):
now = datetime.datetime.now()
invoices = item.task.invoices.copy(handlers=False)
invoices.set_where(id=invoice_id)
invoices.open()
invoices.edit()
invoices.invoice_date.value = now
invoices.post()
invoices.apply()
customer_id = invoices.customer.value
customers = item.task.customers.copy(handlers=False)
customers.set_where(id=customer_id)
customers.open()
customers.edit()
customers.last_modified.value = now
customers.post()
customers.apply()
import datetime
def change_invoice_date(item, invoice_id):
now = datetime.datetime.now()
con = item.task.connect()
try:
invoices = item.task.invoices.copy(handlers=False)
invoices.set_where(id=invoice_id)
invoices.open()
invoices.edit()
invoices.invoice_date.value = now
invoices.post()
invoices.apply(con)
customer_id = invoices.customer.value
customers = item.task.customers.copy(handlers=False)
customers.set_where(id=customer_id)
customers.open()
customers.edit()
customers.last_modified.value = now
customers.post()
customers.apply(con)
con.commit()
finally:
con.close()