dongmi3203 2013-11-24 03:32
浏览 36
已采纳

多个应用程序访问相同的数据

So my team is developing an API in PHP . My main concern is what happen 2 people call to update the same row.

So if one API call will do a read then process info then write, what happens if another API call does the same thing for the same row of data. We are using Postgres OR Mysql.

User has 100 dollars.
API Call 1 to ADD 20 1 reads 100.
API Call 2 to Subtract 20 reads 100.
API Call 1 writes 120;
API Call 2 write 80;( instead of 100);

I know there is a solution involving locking tables or rows, will that solve my problem?

I need a solution where API call 2 doesn't fail, but rather waits or retries.

[EDIT]

I should go in further detail of what we are doing. It's a browser mmo game that where it has 2 parts. A PHP REST API and Java server.

The browser makes AJAX GET API calls ex. : Build Factory for $40k PHP API Check if enough funds, and return JSON. SO -$40k from player.

Our Server ticks have concurrent database calls using boneCP to Update Values like Funds.

So from my previous example CALL 1 would be our PHP API and CALL 2 would be the server ticks.

  • 写回答

2条回答 默认 最新

  • drgaeqdqiiyg14608 2013-11-24 06:49
    关注

    You need to learn about transactions, transaction isolation, and explicit row-level locking:

    select ... for update
    

    In your case, also look into using expressions. Your life would be much simpler if you issued someting like:

    update account set amount = amount - 20 where id = ?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分