Re: locking rows?

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Thu Feb 27 2003 - 10:44:55 EST

  • Next message: Andrus Adamchik: "Re: Primary Key Generation-SQL Server 7.0"

    Locking a row in the database ("pessimistic locking") for an extended
    period of time is a bad idea in most cases (especially in the web
    application). Good solution would've been to use "optimistic locking" -
    doing an update, including the value for the column marked as "locked" in
    the data map in the WHERE clause:

    update bids set highest_price = 101.00
    where highest_price = 100.00

    If update count is zero, it means that somebody else have already updated
    the price, so the application would throw an exception.

    Unfortunately Cayenne will probably support optimistic locking no earlier
    than 1.1 (unless there are volunteers to implement it). For now you may
    have to do raw SQL update and refetch the object fresh (or patch Cayenne).

    Andrus

    > Hi,
    >
    > to avoid going on a wrong way I like to ask you, if there's a better way
    > or maybe my problem isn't one.
    >
    > Imagine a web-application like ebay. You can bet on a product. But it's
    > possible, that there are many bets simultanously from different users.
    > For every bet there has to be a check, if the bet is high enough.
    > Otherwise the user gets an error-message. If i read the product-object
    > for the first user to check and afterwards update the product with the
    > new price, there will be other users, that have read the product from
    > the database before the new price was updated. So there will be no error
    > message for the second user and he will update the same product, but
    > after the first user. So the changes made by the first user will get
    > lost.
    >
    > I thought of a solution by locking the product row, so every following
    > user has to wait until update and freeing of the product. But I can't
    > find any hints for using this in cayenne.
    >
    > Is there any other way to get around this problem?
    >
    > Thanks for any help!
    >
    >
    > Michael



    This archive was generated by hypermail 2.0.0 : Thu Feb 27 2003 - 10:44:55 EST