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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)