I am working on a second-sealed-bid auction web app. I have an AuctionAuction
table to keep auction items on record, a BidBid
table to keep the bids, and a ProxyBidProxyBid
table to keep trace of the highest bids that users have ever placed. Bid and ProxyBid are both associated with Auction, there could be multiple bid records for a given user id and auction id, whereas there can only be one record for a given user id and an auction id. Auction table has a Foreign Key which refers to Bid table, so that I can keep track of the current bid of each auction item.
Bid
and ProxyBid
are both associated with Auction
. There could be multiple bid records for a given user id and auction id, whereas there can only be one record for a given user id and an auction id. The Auction
table has a foreign key which refers to Bid
table, so that I can keep track of the current bid of each auction item.
I have multiple users who may place a bid on the same auction item at the same time, below. Here is the stored procedure that places a bid:
if nobody has placed a bid on the auction item in question
{
insert a record into ProxyBid
insert a record into Bid
update the auction item record, making it reference to the latest Bid record
}
else
{
if the price that the user is bidding at is no higher than the current highest bidding price
{
insert two records into Bid (note: because it is second sealed bid auction)
update the auction item record, making it reference to the latest Bid record
}
else
{
if the user already has a record in ProxyBid
{
insert a record into ProxyBid
}
else
{
update the existing record in ProxyBid
}
insert two records into Bid
update the auction item record, making it reference to the latest Bid record
}
}
I enclose if-else blocks inside try-catch block and transaction block. I am new to SQL, and I am always curious that, would it be better to do it in the other way, which is to enclose one try-catch block and transaction block inside each if-else block.
The other thing is, the control flow depends on the highest bidding price at the execution time. So, if another user inserts another record with higher price in middle of the execution, the data will be inconsistent. Will the default isolation level of transaction prevent that from happening?
My 3rd question is, is there a better way to handle error/exception, and pass them to client code such as C#?
if nobody has placed a bid on the auction item in question { insert a record into ProxyBid insert a record into Bid update the auction item record, making it reference to the latest Bid record } else { if the price that the user is bidding at is no higher than the current highest bidding price { insert two records into Bid (note: because it is second sealed bid auction) update the auction item record, making it reference to the latest Bid record } else { if the user already has a record in ProxyBid { insert a record into ProxyBid } else { update the existing record in ProxyBid } insert two records into Bid update the auction item record, making it reference to the latest Bid record } }
Thanks a lot.
- I enclose if-else blocks inside try-catch block and transaction block. I am new to SQL, and I am always curious about whether it would be better to do it in the other way, which is to enclose one try-catch block and transaction block inside each
if
-else
block. - The control flow depends on the highest bidding price at the execution time. So, if another user inserts another record with higher price in middle of the execution, the data will be inconsistent. Will the default isolation level of transaction prevent that from happening?
- Is there a better way to handle error/exception, and pass them to client code such as C#?