begin; INSERT INTO transaction VALUES(xid, $seller_id, $buyer_id, $amount); put_to_queue "update user("seller", $seller_id, amount); put_to_queue "update user("buyer", $buyer_id, amount); commit; for each message in queue begin; dequeue message; if message.type = "seller" then UPDATE user SET amt_sold = amt_sold + message.amount WHERE id = message.user_id; else UPDATE user SET amt_bought = amt_bought + message.amount WHERE id = message.user_id; end commit; end
begin; INSERT INTO transaction VALUES(xid, $seller_id, $buyer_id, $amount); put_to_queue "update user("seller", $seller_id, amount); put_to_queue "update user("buyer", $buyer_id, amount); commit; for each message in queue begin; SELECT count(*) as cnt FROM message_applied WHERE msg_id = message.id; if cnt = then if message.type = "seller" then UPDATE user SET amt_sold = amt_sold + message.amount WHERE id = message.user_id; else UPDATE user SET amt_bought = amt_bought + message.amount WHERE id = message.user_id; end INSERT INTO message_applied VALUES(message.id); end commit; if 上述事務成功 dequeue message DELETE FROM message_applied WHERE msg_id = message.id; end end