I am trying to maintain some cross transactional-ity between different services.
Basically I have a ndb model which would be processed. There is a chance that the transaction might fail after we have saved an object. How would I rollback in such a scenario?
Example
class PersonTable(ndb.Model):
personId = ndb.StringProperty()
personName = ndb.StringProperty()
personAddress = ndb.StringProperty()
personOldReference = ndb.StringProperty()
scopeReference = ndb.StringProperty()
def save():
self.put()
def some_method(person_id, person_name):
person = PersonTable.get(person_id)
person.personName = person_name
sql_db.saveRelated(person_id, personName)
person.save()
@ndb.transactional
def outer_method(person_id, person_name):
person_name = prefix+person_name
some_method(person_id, person_name)
post_update_tasks(person_id)
Now I was thinking of something like
def some_method(person_id, person_name):
person = PersonTable.get(person_id)
person.personName = person_name
try:
sql_db.saveRelated(person_id, personName)
person.save()
except Exception as e:
sql_db.rollback(person_id)
raise e
This would solve the problem if save
fails. But if something fails in the outer_method
after, then the transactional fails and ndb will roll back but sql_db
would not be able to rollback.
@ndb.transactional
def outer_method(person_id, person_name):
person_name = prefix+person_name
some_method(person_id, person_name) ---> This is sucessfull & data is saved in both ndb & sql.
post_update_tasks(person_id) --> If this fails then ndb is rolled back but not SQL.
How would I solve for this problem? Is there a hook on the model similar to _pre_put_hook
which would execute if theres a rollback on ndb?