1

I have an application that uses its own database and also works with remote accounting system through http post requests/responses base on XML. I can post debit and credit http post requests.

When some user plays a game i have to credit him with some stake and if he wins i have to debit the win amount. Since it's http protocol i implemented some timeout-retry functionality, but i want to ask how to handle the case when http request was fine and i got a response, but after that something failed at my side? It is like distributed transaction case, but not the same :(...

Some pseudo code to be more clear:

public void handlePlay(double stake, double winAmount, int playerId) {
    // post http stake request to remote account system
     stakePlayer(playerId, stake); // this method post http request and on fail throws exception
     int outcomeId;
    // persists game outcome with stake and winAmount in local database
     try {
       outcomeId = persistOutcome(stake, winAmount, playerId);// this is in separate transaction 
     } catch(Excetion e) {
        // send http post request to cancel stake request
        cancelStake(stake, playerId);
     }

     if (winAmount > 0.0) { // send win amount http request
        try {
          winPlayer(winAmount, playerId); // debits winamount remote account system
        } catch (Exception e) {
        // send http post request to cancel stake request
          cancelStake(stake, playerId);              
        // call method which deletes the outcome
          deleteOutcome(outcomeId);
          throw e;// finally this exception is thrown to the player
        }
     }
}

The problem in this code is: 1) First if persistOutcome(..) fails and in the catch clause cancelStake is called it may fail to send cancel request (even it has retry algoritym). 2) The same is applied for second catch block.

Please suggest best practices for such case.

user358448
  • 1,127
  • 3
  • 20
  • 39
  • 1
    Thirteen questions and you did not accept one answer! It's more likely people will help you if you accept some answers. – home Nov 14 '11 at 12:41
  • 1
    Compensation based transaction model like WS-BA. http://stackoverflow.com/questions/434950/transaction-rollback-and-web-services – Uncredited Nov 14 '11 at 13:49

0 Answers0