Re: transactions, row locking, race condition?

From: tnaki..ofthome.net
Date: Mon Apr 11 2005 - 04:03:55 EDT

  • Next message: tnaki..ofthome.net: "Re: calling stored procedures"

    Andrus Adamchik wrote:

    >
    > On Apr 9, 2005, at 10:21 AM, tnaki..ofthome.net wrote:
    >
    >> The problem: N concurrent users access try to read and modify M table
    >> rows and no two users should get the same table row (using Postgre 8).
    >
    > Can you elaborate? Give an example?

    Of course. It's about a phone polling system. Several operators work
    concurrently, each repetatively loading new contacts to poll from the
    database as they work.
    My idea for loading the next contact goes something like this:
    1. begin transaction
    2. select 1 poll candidate from candidates FOR UPDATE (to lock the record)
    3. update a timestamp signifying that this candidate is taken
    4. retrieve the candidate
    5. end transaction

    The purpose of step 3 is to make sure the database-level lock lasts as
    short as possible: contacts with a timestamp within the last couple of
    minutes are filtered out in step 2.
    Basically, what I'd like to do with cayenne is to make "select ... for
    update" queries by each operator to make sure every operator receives a
    unique person: the goal is to prevent the same name to appear on two
    client machines.
    Hope I did a better job explaining this time.

    Gratefully,
    Tomislav



    This archive was generated by hypermail 2.0.0 : Mon Apr 11 2005 - 03:59:59 EDT