Firebase数据库:
{
"balance" : 10
}
应用程序1(伪码):
if (balance-10 >= 0)
{
// Application freezes for a few seconds. (Connection/hardware issue or whatever reason)
balance -= 10;
}
应用程序2(伪码):
if (balance-10 >= 0)
{
// Application is executed without any problems.
balance -= 10;
}
如果两个应用程序同时启动,则最终的"balance“值将是"-10”而不是"0“。
在MySQL中,这个问题很容易通过以下方法解决:
更新
table
集balance=IF(balance-10>=0,balance-10,balance);
Firebase可能的解决方案是什么?
发布于 2017-08-18 02:15:41
这是通过交易记录处理的。由于您没有指定我显示的web代码是什么技术:
balanceRef.transaction(function(current) {
if (current && current > 10) {
return current - 10;
}
return (current || 0);
}
如果多个应用程序同时运行此代码,其中一个应用程序的事务将被拒绝并重试。
另见:
https://stackoverflow.com/questions/45747224
复制相似问题