Mutex access on a database?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Godfrey

    Mutex access on a database?

    Hello. I have a database which I have abstracted off with a set of
    functions I have written. (So all the correct quoting and unquoting are all
    in one place.)

    /* Simplified code. */

    $tokenCount = getTokenCount($ userIdx);
    /* Context switch! */
    setTokenCount($ userIdx, $tokenCount + 1);

    But what if another process perfoms a get on the same record between the
    get and the set of the first process? What I could do with is a mutex.

    Do they please exist for PHP running under Apache?

    Bill, lost update.

    --
    http://billpg.me.uk/ usenet(at)billp g(dot)me(dot)uk
  • Gordon Burditt

    #2
    Re: Mutex access on a database?

    >Hello. I have a database which I have abstracted off with a set of[color=blue]
    >functions I have written. (So all the correct quoting and unquoting are all
    >in one place.)
    >
    >/* Simplified code. */
    >
    >$tokenCount = getTokenCount($ userIdx);
    >/* Context switch! */
    >setTokenCount( $userIdx, $tokenCount + 1);
    >
    >But what if another process perfoms a get on the same record between the
    >get and the set of the first process? What I could do with is a mutex.
    >
    >Do they please exist for PHP running under Apache?[/color]

    If you perform a query like:

    update foo set bar=bar+1 where id = '$userIdx';

    it should be atomic in the database without the need for explicit
    locks, as it's all done in one SQL statement.

    If your abstractions provide only the ability to read and write
    things, not atomic increments and decrements, perhaps your design
    needs changing.

    Gordon L. Burditt

    Comment

    • Bill Godfrey

      #3
      Re: Mutex access on a database?

      gordon@hammy.bu rditt.org (Gordon Burditt) wrote:[color=blue]
      > If you perform a query like:
      > update foo set bar=bar+1 where id = '$userIdx';[/color]

      Ah ha. I must confess, I only know SQL from cribbing the commands
      phpMyAdmin creates. I didn't realise you could do that.
      [color=blue]
      > it should be atomic in the database without the need for explicit
      > locks, as it's all done in one SQL statement.[/color]

      If the database server (MySQL) will enforce atomic access, thats perfect.
      [color=blue]
      > If your abstractions provide only the ability to read and write
      > things, not atomic increments and decrements, perhaps your design
      > needs changing.[/color]

      I'm still in the poke-about-with-prototype-code phase, so no worries.
      Thanks.

      Bill, incremental.

      --
      http://billpg.me.uk/ usenet(at)billp g(dot)me(dot)uk

      Comment

      • Colin McKinnon

        #4
        Re: Mutex access on a database?

        Bill Godfrey wrote:
        [color=blue]
        > gordon@hammy.bu rditt.org (Gordon Burditt) wrote:[color=green]
        >> If you perform a query like:
        >> update foo set bar=bar+1 where id = '$userIdx';[/color]
        >
        > Ah ha. I must confess, I only know SQL from cribbing the commands
        > phpMyAdmin creates. I didn't realise you could do that.
        >[color=green]
        >> it should be atomic in the database without the need for explicit
        >> locks, as it's all done in one SQL statement.[/color]
        >
        > If the database server (MySQL) will enforce atomic access, thats perfect.
        >[/color]

        Presumably because you want to read either the prev or new value? In that
        case you'll need to lock the tables first:

        LOCK TABLES foo READ;
        SELECT bar FROM foo WHERE id = '$userIdx';
        UPDATE foo SET bar=bar+1 WHERE id = '$userIdx';
        SELECT bar FROM foo WHERE id = '$userIdx';
        UNLOCK TABLES;

        (quick hack, not tested YMMV)

        C.

        Comment

        Working...