【问题标题】:GetAll() in a cloud.google.com/go/datastore Transaction{}?Cloud.google.com/go/datastore 中的 GetAll() 事务{}?
【发布时间】:2021-09-22 11:52:26
【问题描述】:

我需要运行一个等价的,但是在一个事务中: db.GetAll(ctx, datastore.NewQuery("Items").Ancestor(pkey), &itemContainers)

Transaction{} 类型似乎没有GetAll() 方法。我怎样才能完成这项工作?

【问题讨论】:

    标签: google-app-engine google-cloud-datastore google-app-engine-golang


    【解决方案1】:

    要在事务中使用查询,您应该附上您的transaction to the query。例如

    q = datastore.NewQuery("Items").Ancestor(pkey).Transaction(tx)
    db.Getall(ctx, q, &itemContainers)
    

    【讨论】:

      【解决方案2】:

      您不需要GetAll() 的事务,您可以简单地在您的*Client 变量上调用它。

      事务用于需要同时运行或回滚的一组原子操作。例如,如果您有一组需要获取的密钥,您可以在 Transaction 类型上调用 GetMulti()https://pkg.go.dev/cloud.google.com/go/datastore#Transaction.GetMulti

      GetAll 可以通过单个操作完成,不需要在事务中批处理:https://pkg.go.dev/cloud.google.com/go/datastore#Client.GetAll

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-22
        • 1970-01-01
        • 2018-07-18
        • 2020-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多