Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL Server Mutex?

Author  Topic 

dlouche
Starting Member

9 Posts

Posted - 2009-02-02 : 10:57:31
I’m working on an auction like website where in the database I am storing user bids

Bids
----------------------------------
int ID
int UserID
decimal BidAmount

The bid amount is to be determined automatically based on the previous bid amount (i.e. each bid equals the previous bid plus a specific amount).

When a user bids, I want to query the table to determine the current highest bid amount, and then use that data to insert a new bid. However I am concerned about a race condition between the query for the highest bid and the insert of the new bid (e.g. user A queries to determine the highest bid is $10, user B inserts a high bid of $10.50, then user A inserts a bid of $10.50, when it should have been $11).

So I want to block a ‘bidding’ user from querying the table (but non-bidding users should not be blocked) until any active bids are complete.

My guess at how to accomplish this would be to use a mutex (I’ve only been able to find one decent article on this) in the bid operation, but I wanted to get your guys opinion (and references/examples?).

Thanks

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2009-02-02 : 11:03:13
which decent article?

i think you should solve this at the app level.
if you must do this in db the use a sproc with this method:
http://www.sqlteam.com/article/application-locks-or-mutexes-in-sql-server-2005

___________________________________________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.1 out!
Go to Top of Page

dlouche
Starting Member

9 Posts

Posted - 2009-02-02 : 11:10:12
That's the article I was referring to!

I can solve it in the app with a mutex, but I thought a db solution might be more efficient. But that's not based on anything but hope.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2009-02-02 : 11:14:00
i would suggest you solve it in the app then.

___________________________________________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.1 out!
Go to Top of Page
   

- Advertisement -