0

I have a complex web site that handles online games with real money. I use a double-entry database design for my transactions, a simple example is as follows:

John deposits $5 John receives 5000 credits John uses those 5000 credits to play a game.

The transaction in the database looks as follows:

trans_id | account_id  | trans_type | date | amount |
-----------------------------------------------------
1        | John(PayPal)| Debit      | date | -5.00  |
2        | System      | Credit     | date |  5.00  |
3        | SystemGame  | Debit      | date | -5000  |
4        | JohnGame    | Credit     | date |  5000  |

I wrote a stored procedure with a transaction in it that inserts transactions 1 and 2, the Debit from John's PayPal account, and the Credit to our System account.

My question is, should I also include the other transactions where John has money transferred from our SystemGame account to his Game account? Or should I have a stored procedure for each group of transactions? All 4 transactions occur simultaneously, John is credited immediately after depositing the $5.

Also, should I separate the transaction tables for game credits from the real money transaction table?

user1143767
  • 1,461
  • 2
  • 13
  • 13

2 Answers2

0

I think this is what you want. But I recommend you doing in 2 different transactions to separate ingame-money and real-money.

The only reason is for if in future you need to change ingame-money management or real-money management you can do it separately.

Pablo Martinez
  • 2,172
  • 1
  • 23
  • 27
0

A transaction must be atomic: all the steps must be completed. If one fails, everything is rolled back.

If those 4 steps you mention must be all completed at once, they must be in a single transaction.

So, for me, your current approach is OK.

However, if you want to split things into 2 stored procedures, you can manage transactions from the language(PHP, C#) you use.

Check this: Transactions in MySQL - Unable to Roll Back and this: PHP + MySQL transactions examples

Community
  • 1
  • 1
Nathan
  • 2,705
  • 23
  • 28