T
The Crow
we are developing an ASP.NET application with SqlServer at backend..
there are an supplier and about 3000 customer, and each customer has about
3-4 users. they are selling mobile phone counters.
while a sale occurs, we are selecting customers stock amount, if it is
bigger then sale amount, we are updating its stock, and we increment
suppliers stock. The query is like that :
DECLARE @StockAmount int
SELECT @StockAmount = Amont FROM Stocks WHERE CustomerId = @BuyerCustomerId
IF @StockAmount > @SaleAmount
UPDATE Stocks SET amount = @StockAmount - @SaleAmount WHERE CustomerId =
@BuyerCustomerId
SELECT @SuppliersStockAmount = Amont FROM Stocks WHERE CustomerId =
@SellerCustomerId
UPDATE Stocks SET amount = @StockAmount + @SaleAmount WHERE CustomerId =
@SellerCustomerId
i know, each customer has got not so many users and a concurrency problem
seems to be not a big possibility. but there is just a 1 supplier record and
i think conlicts are possible. how can i alter this problem. after a
research, i found
SELECT @SuppliersStockAmount = Amont FROM Stocks WITH (XLOCK ROWLOCK) WHERE
CustomerId = @SellerCustomerId
seems to work fine for us, but it will block the row untill the transaction
finishes. any approaches are appreciated. thanks.
there are an supplier and about 3000 customer, and each customer has about
3-4 users. they are selling mobile phone counters.
while a sale occurs, we are selecting customers stock amount, if it is
bigger then sale amount, we are updating its stock, and we increment
suppliers stock. The query is like that :
DECLARE @StockAmount int
SELECT @StockAmount = Amont FROM Stocks WHERE CustomerId = @BuyerCustomerId
IF @StockAmount > @SaleAmount
UPDATE Stocks SET amount = @StockAmount - @SaleAmount WHERE CustomerId =
@BuyerCustomerId
SELECT @SuppliersStockAmount = Amont FROM Stocks WHERE CustomerId =
@SellerCustomerId
UPDATE Stocks SET amount = @StockAmount + @SaleAmount WHERE CustomerId =
@SellerCustomerId
i know, each customer has got not so many users and a concurrency problem
seems to be not a big possibility. but there is just a 1 supplier record and
i think conlicts are possible. how can i alter this problem. after a
research, i found
SELECT @SuppliersStockAmount = Amont FROM Stocks WITH (XLOCK ROWLOCK) WHERE
CustomerId = @SellerCustomerId
seems to work fine for us, but it will block the row untill the transaction
finishes. any approaches are appreciated. thanks.