Page 1 of 1

Multiple Submits

Posted: Sat Jul 29, 2006 11:41 pm
by StormTheGates
Hey everyone. I run a text based game and lately I have been having some serious problems. The whole problem stems from my slow server speed. Heres what it is:

Say a user has 1000 dollars. They open up two windows, and on one page go to bank, and on the other go to say roulette. They deposit 1000 dollars into their bank at nearly the exact same time as they bet it on roulette. This has the effect of cloning their money, because it puts it in the bank and bets it at the same time.

I was wondering if anyone had any ideas how to stop this? Right now Iam trying to set a session variable to like 3 seconds, then at the very top of my db connector I check it, and see if they have to wait. I was just wondering what you guys thought?

Posted: Sun Jul 30, 2006 1:12 am
by Charles256
erm..have you tried temporarly "locking" the table that keeps track of their mnoey while you modify it. effectively making it impossible to mess with your bank until your gambling is done. that should do the trick. :-D

Posted: Sun Jul 30, 2006 4:34 am
by AKA Panama Jack
You should be deducting the money from the players database entry immediately before you do anything else on the bank or roulette. You might want to place an IF check on the field as well to make sure the value being deducted will not go below 0.

And table locking is messy stuff and can cause the database server to eventually lockup if you are not careful in how you lock and unlock rows. And if the database server is already slow table locking will make it seem even slower. If you want automatic row locking on mysql databases change your database from MYISAM to INNODB. The INNODB format does automatic row locking. This doesn't lock the entire table but locks the row being accessed.

Posted: Sun Jul 30, 2006 4:38 am
by Charles256
touche. : tips his hat to him. : :-D

Posted: Sun Jul 30, 2006 9:56 am
by Ambush Commander
This is a transactional problem, where a transaction requires multiple selects/updates.
You should be deducting the money from the players database entry immediately before you do anything else on the bank or roulette. You might want to place an IF check on the field as well to make sure the value being deducted will not go below 0.
While this can only hurt the player than help them, you've still got the race condition where if they abort before anything else is done, the money deducted will just have disappeared into nothingness.
And table locking is messy stuff and can cause the database server to eventually lockup if you are not careful in how you lock and unlock rows. And if the database server is already slow table locking will make it seem even slower. If you want automatic row locking on mysql databases change your database from MYISAM to INNODB. The INNODB format does automatic row locking. This doesn't lock the entire table but locks the row being accessed.
InnoDB supports transactions. Use them.

Posted: Sun Jul 30, 2006 1:04 pm
by StormTheGates
Hey everyone, thanks for all the awsome responses :D.

Iam a real nooblet when it comes to PHP, so what exactly is locking? I figure that it prevents a table from being accessed, but for how long? Because I make multiple SQL queries on one page, and it wont do any good if the thing is locked when I need to update the users money or somesuch.

Posted: Sun Jul 30, 2006 3:52 pm
by AKA Panama Jack
StormTheGates wrote:Iam a real nooblet when it comes to PHP, so what exactly is locking? I figure that it prevents a table from being accessed, but for how long? Because I make multiple SQL queries on one page, and it wont do any good if the thing is locked when I need to update the users money or somesuch.
Read up on it here...

http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html

Posted: Sun Jul 30, 2006 3:59 pm
by AKA Panama Jack
Ambush Commander wrote:This is a transactional problem, where a transaction requires multiple selects/updates.
You should be deducting the money from the players database entry immediately before you do anything else on the bank or roulette. You might want to place an IF check on the field as well to make sure the value being deducted will not go below 0.
While this can only hurt the player than help them, you've still got the race condition where if they abort before anything else is done, the money deducted will just have disappeared into nothingness.
Actually that isn't going to be a problem. :) I should know our game can allow you to use multiple windows and these things will not happen if you program the queries properly. We do not use table locking because it CAN cause a multitude of problems. We have tried it a multitude of times and every time if the server has moderate usage the database server becomes backed up with queries and everyone trying to access the game starts slowing down. It is definitely not worth the trouble it causes.
Ambush Commander wrote:
And table locking is messy stuff and can cause the database server to eventually lockup if you are not careful in how you lock and unlock rows. And if the database server is already slow table locking will make it seem even slower. If you want automatic row locking on mysql databases change your database from MYISAM to INNODB. The INNODB format does automatic row locking. This doesn't lock the entire table but locks the row being accessed.
InnoDB supports transactions. Use them.
There is only one problem I have with INNODB tables. The amount of memory the tables use when being accessed is many times more than if the table is in MYISAM format. If the tables are LARGE in size you will need a huge amount of system ram to prevent the database from using a lot of swapfile and slowing down the database server. I would only use INNODB tables if the tables are small in size.

Posted: Sun Jul 30, 2006 10:58 pm
by StormTheGates
Ive got 2 gigs of system RAM, should I upgrade to 4?

Posted: Mon Jul 31, 2006 1:05 am
by AKA Panama Jack
You should be ok but if you have multiple sites that use INNODB tables on there you might need more. You should monitor the amount of memory used by mysql. If you notice you are using swap file space too often then you might want to think about adding more memory.

Posted: Mon Jul 31, 2006 2:24 am
by jmut
AKA Panama Jack wrote:
Ambush Commander wrote:This is a transactional problem, where a transaction requires multiple selects/updates.
You should be deducting the money from the players database entry immediately before you do anything else on the bank or roulette. You might want to place an IF check on the field as well to make sure the value being deducted will not go below 0.
While this can only hurt the player than help them, you've still got the race condition where if they abort before anything else is done, the money deducted will just have disappeared into nothingness.
Actually that isn't going to be a problem. :) I should know our game can allow you to use multiple windows and these things will not happen if you program the queries properly. We do not use table locking because it CAN cause a multitude of problems. We have tried it a multitude of times and every time if the server has moderate usage the database server becomes backed up with queries and everyone trying to access the game starts slowing down. It is definitely not worth the trouble it causes.
I would really like to see how you prevent "money loss" in this example with just programming queries properly. This is a transaction problem and if you don't use one...things gets messy in DB (in your pocket).

Posted: Mon Jul 31, 2006 2:41 am
by AKA Panama Jack
It has never happened and we can track that in our game. :) We have something called extended logging that records every single get and post array each player sends to the server. The code for the bank and casino is very tight. People have definitely tried to fake it out but as I said if you keep things tight and use the proper checks inside the queries you will not have a transactional problem.

Posted: Mon Jul 31, 2006 3:05 am
by jmut
AKA Panama Jack wrote:It has never happened and we can track that in our game. :) We have something called extended logging that records every single get and post array each player sends to the server. The code for the bank and casino is very tight. People have definitely tried to fake it out but as I said if you keep things tight and use the proper checks inside the queries you will not have a transactional problem.
can you give me a link of this bank/casino (maybe PM not to be considered Ad)