SQL Server - Simultaneous Inserts to the table from multiple clients - Check Limit and Block

我怕爱的太早我们不能终老 提交于 2019-12-10 14:49:09

问题


We are recently facing one issue with simultaneous inserts into one of our sal server tables from multiple clients. I hope you guys can help us through.

We are using stored procedure to do the transactions. In that stored procedure, for each transaction, we calculate total sales so far. If the total sales is less than the set limit, then the transaction will be allowed. Otherwise, the transaction will be denied.

it works fine most of times. But, sometimes when multiple clients trying to do the transaction exactly at the same time, the limit check is failing as both the transactions get done.

Can you guys suggest how we can effectively enforce the limit all the time? Is there any better way to do that?

Thanks!


回答1:


I don't think it is possible to do this declaratively.

If all inserts are guaranteed to go through the stored procedure and the SaleValue is not updated once inserted then the following should work (I made up table and column names as these were not supplied in the initial question)

DECLARE @SumSaleValue MONEY

BEGIN TRAN

SELECT @SumSaleValue = SUM(SaleValue)
FROM dbo.Orders WITH (UPDLOCK, HOLDLOCK)
WHERE TransactionId = @TransactionId

IF @SumSaleValue > 1000
    BEGIN
    RAISERROR('Cannot do insert as total would exceed order limit',16,1);
    ROLLBACK;
    RETURN;
    END

/*Code for INSERT goes here*/

COMMIT

The HOLDLOCK gives serializable semantics and locks the entire range matching the TransactionId and the UPDLOCK prevents two concurrent transactions locking the same range thus reducing the risk of deadlocks.

An index on TransactionId,SaleValue would be best to support this query.



来源:https://stackoverflow.com/questions/14913953/sql-server-simultaneous-inserts-to-the-table-from-multiple-clients-check-lim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!