方法失败后如何在Google App Engine NDB中回滚

时间:2017-05-17 10:46:55

标签: python google-app-engine app-engine-ndb

我在Google App Engine上部署了一个项目....我正面临回滚问题。我想像关系数据库一样回滚,我们在返回或结束方法之前提交。我们如何在ndb中实现这一点?

为清晰起见,请在下面找到代码段

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")

假设我的函数在返回之前和第二次放置之后失败,我该如何回滚???

由于我不熟悉Google App Engine和NDB,因此非常感谢帮助

1 个答案:

答案 0 :(得分:0)

不确定我理解。在最后一次放置之后和返回之前它怎么会失败?

也许您想在事务中运行代码:

https://cloud.google.com/appengine/docs/standard/python/ndb/transactions

您可以在事务中运行它,并在需要时调用ndb.Rollback。