How to rollback in Google App Engine NDB after method fails

4 views
Skip to first unread message

Pravin Agre via StackOverflow

unread,
May 17, 2017, 6:48:06 AM5/17/17
to google-appengin...@googlegroups.com

I have a project deployed on Google App Engine....I am facing rollback issue. I want to rollback just like Relational db where we do commit before return or end of method. How do we achieve this in ndb???

Please find below code snippet for clarity

class PersonTable(ndb.Model):
    personId = ndb.StringProperty()
    personName = ndb.StringProperty()
    personAddress = ndb.StringProperty()
    personOldReference = ndb.StringProperty()
    scopeReference = ndb.StringProperty()    

class ScopeTable(ndb.Model):
    child = ndb.StringProperty() 
    isPerson = ndb.BooleanProperty()
    parent = ndb.StringProperty()

@endpoints.method(ZGScopeRequest, ZGScopeResponse, name='scope', path='scope', http_method='POST')
def scope(self, request):

    scope = request.scope
    person = request.personName
    list = scope.split('.')
    length = len(list)
    for entry in list:
        newId = GenerateRandomId('SCOPE')   
        child = mobileappmodels.ScopeTable.query(mobileappmodels.ScopeTable.child.IN([entry])).get()
        if child:
            pass
        else:
            index = list.index(entry)
            parent = list[index-1]
            if entry == 'PersonName':
                entry = person
                fetchPerson = mobileappmodels.CommunityTable.gql("""WHERE personName = :1""",entry).fetch()
                update = fetchCommunity[0].key.get()

                update.scopeReference = newId
                update.put()

                save = mobileappmodels.ScopeTable(child=person, isCommunity = True, parent = parent, id=newId)
                save.put()

            elif entry == 'Global':
                save = mobileappmodels.ScopeTable(child=entry, isCommunity = False, parent = '', id=newId)
                save.put()
            else:
                save = mobileappmodels.ScopeTable(child=entry, isCommunity = False, parent = parent, id=newId)
                save.put()

    return ZGScopeResponse(message="Uploaded")

Suppose my function fails just before return and after 2nd put, how do I rollback???

Help would be really appreciate as I am new to Google App Engine and NDB



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/44022687/how-to-rollback-in-google-app-engine-ndb-after-method-fails
Reply all
Reply to author
Forward
0 new messages