我們正在開發一款IOS遊戲,並在後端使用google-app-engine。用戶可以通過點擊建築物來獲得遊戲中的錢。用戶以相當快的速度敲打幾座建築物,這會導致同一對象(USER)和不同建築物上的多個併發交易。多筆交易,併發性和性能
不幸的是,這意味着4-5分接頭因爲失去重試而超時。這也意味着由於鎖定原因,第2至第5個抽頭非常緩慢。
我的第一個想法是讓交易成爲一項任務,但是第二次調用函數時totalAmount會出錯,因爲第一次調用還沒有完成。
那麼支持多個快速更新到同一實體的任何好方法?
int retries = 5;
while (retries > 0) {
// Wrap everything into a transaction from here
TransactionOptions options = TransactionOptions.Builder.withXG(true);
Transaction txn = datastore.beginTransaction(options);
try{
// Ok we got the template - now check against the
// Update user... with money gained
// Update Building...money withdrawn
//Do the transaction
datastore.put(txn, user.getEntity());
datastore.put(txn, building.getEntity());
txn.commit();
// do callback code...which returns TotalMoney
break;
} catch (Exception e) {
if(retries > 0){
retries--;
}
else{
//fail code...
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
}
是否有任何不使用memcache的原因? – greg 2012-04-24 16:58:19
好的用戶競賽條件。用戶可以在1秒鐘內「挖掘」多個建築物並緩存資金。沒有交易可能會導致用戶「鬆動」水龍頭。 – Middy 2012-10-29 12:57:44