【问题标题】:How to rollback in Google App Engine NDB after method fails方法失败后如何在 Google App Engine NDB 中回滚
【发布时间】:2017-05-17 10:46:55
【问题描述】:

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

为了清楚起见,请在下面找到代码 sn-p

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 的新手

【问题讨论】:

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


    【解决方案1】:

    不确定我是否理解。怎么会在最后一次put之后return之前失败呢?

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

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

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

    【讨论】:

    • 你是对的,为什么它会在返回之前和之后失败......从技术上讲它不会失败......我的意思是如果它在第二次失败时如何回滚。
    • 我尝试使用事务,但它返回以下错误 BadRequestError:事务中只允许祖先查询。
    猜你喜欢
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    相关资源
    最近更新 更多