dongxu198714 2012-02-06 13:57
浏览 21
已采纳

如何在Go中进行更新? (Google App Engine)

I need to update a datastore entity in a way that will not be broken by multiple concurrent users doing the same thing.
I understand that I can't use SQL for updating the datastore but I'm not sure what else would work.

This is how I would achieve it in an RDBMS using SQL:

-- Account.Balance   = current balance
-- Account.Rate      = increase per second
-- Account.CheckDate = the last time the balance was checked and updated

-- so we need to find the number of seconds since the last check, 
-- update the balance by rate*seconds, then update the check datetime

UPDATE    Account
SET       Account.Balance = Account.Balance + ( DATEDIFF(S, GETDATE(), Account.CheckDate) * Account.Rate),
          Account.CheckDate = GETDATE()

I know that I can wrap all the operations in a single transaction, but how can I ensure that the update is not miscalculated because of multiple users without using a single update operation like the SQL shown?
You can probably see that several operations like:

1. Read entity
2. Update values
3. Save entity

might fail because of several users doing the same thing

I'm guessing there are several possible ways to achieve this and I'm looking for the one which would work best for this and future requirements.

================== ANSWER ==================

When I got to this point I realised that as long as I update the balance and the check date at the same time, all will be fine. Concurrent updates will not break anything :)

But I thought I'd post it anyway!

I'm still happy to see better solutions though...

  • 写回答

2条回答 默认 最新

  • dtrn74832 2012-02-07 05:43
    关注

    Transactions may be used to atomically execute a sequence of datastore operations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?