1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Rep Power

    Database locking/transactions techniques for browsergame PHP/MySQL

    Hi everyone,

    I'm wondering what's the proper way to handle concurrent actions in a browsergame, that access the same resource (e.g. one player's warehouse), combined with the "on view" method instead of cronjob.

    Basically that means, instead of running some script that processes the action queues each second, the calculations are made when that resource (warehouse) is accessed the next time (e.g. one attacks and take resources, or the player itself spends some of them).

    Problem obviously occurs when two players access the same resource at the same time, and I'm not sure if there's a good-proven standard way to implement this...

    Some guesses of me would be:

    1. Using MySQL transactions (one transactions includes: read from actions table, execute them, delete executed actions)
    2. Using MySQL table lock for actions table
    3. Locking with PHP flock()
    4. Using semaphore with PHP sem_acquire()

    Any suggestions?
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Rep Power
    Use database transactions, possibly with an explicit lock if you need to run some code at the application layer between a read and a write to the database.

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo