473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Lost Update Problem?

Hi everybody,

I am writing a scheduling system that has appointments table. For each
appointment I am adding a new record that has EventID and ClientID. Each
event has capacity that needs to be checked before adding new appointments,
if it is reached no new appointments could be added. This logic is
implemented as a stored procedure.

Problem that I am trying to solve is concurrent scheduling -- when two or
more clients are trying to schedule an event, it is possible that they would
check capacity all together before adding new records to appointments table,
get permission and then create appointments, exceeding capacity.

Simply wrapping capacity checking and adding new appointment into explicit
transaction does not help. The best solution that I found so far is adding
following statement in the beginning of the transaction:

SELECT 0 FROM Appointments WITH(TABLOCKX)

It effectively locks the whole table till the end of the transaction thus
insuring that until the first client checked and updated table nobody else
can access it. Are there any better solutions? Is there any explicit
statements in MS SQL to lock table?

Any advice is really appreciated.

Thanks,
Anatoly

Here is the code:

BEGIN TRAN

/* If uncommented following statement solves the problem */
/* SELECT 0 FROM Appointments WITH(TABLOCKX) */

IF dbo.GetAvailableSpotsNum(@EventID) <= 0
BEGIN
ROLLBACK TRAN
RETURN
END

INSERT INTO Appointments
(
Client,
Event
)
VALUES
(
@ClientID,
@EventID
)

COMMIT TRAN

And function GetAvailableSpotsNum:

FUNCTION dbo.GetAvailableSpotsNum
(
@EventID INT
)
RETURNS INT
AS
BEGIN
DECLARE @RES INT

SELECT @RES = Capacity - (SELECT COUNT(*) FROM Appointments WHERE Event =
@EventID)
FROM Events WHERE EventID = @EventID

RETURN @RES
END
Jul 20 '05 #1
1 4588
On Thu, 14 Oct 2004 23:24:45 -0400, Anatoly wrote:
Problem that I am trying to solve is concurrent scheduling -- when two or
more clients are trying to schedule an event, it is possible that they would
check capacity all together before adding new records to appointments table,
get permission and then create appointments, exceeding capacity.


Hi Anatoly,

You can solve this by enclosing the read operation (to check available
places) and the update operation (to actually schedule the event if places
are available) in a transaction AND setting the transaction isolation
level to repeatable read or serializable.

You didn't post the code of the UDF, so I can't tell which of the two
isolation levels you need. Repeatable read locks the rows read until the
end of the transaction, preventing updates from other connections.
Serializable locks not only the rows read, but a complete key range,
ensuring no other rows are inserted that would have met the WHERE-clause
of the SELECT statement(s) used. Both have less impact on concurrency than
locking an entire table.

You'll also need to change the UDF. If two connections read and lock the
same data (which is possible, as a SELECT uses a shared lock by default),
neither will be able to upgrade that lock to an exclusive lock. The UDF
should provide the UPDLOCK locking hint. That will force the SELECT to
acquire exclusive locks instead of shared locks.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nipon | last post by:
Hi, I am using MS SQL Server 7.0 SP2 in Windows 2000 server SP4. I have one-to-many tables (TABLE_HEAD and TABLE_DETAILS)which I am going to update by using a stored procedure with UPDATE...
0
by: thomaz | last post by:
I use the DataSource like below to fill a Combobox: comboBox1.DataSource = dataSet1.Tables; comboBox1.DisplayMember = "ProductName"; When the user types any keyboard key i use the DROPDOWN...
0
by: Dailan | last post by:
Hi, I create a tree view. Each node has link button associate with it, which includes add, edit, delete buttons. The way I did is for users who have very low capablity of using computer. Now I...
4
by: Rob Kroese | last post by:
I've got a form with a datagrid that displays a list of items, along with several textboxes, comboboxes, etc., that display the details for the selected item. The columns in the datagrid and the...
14
by: Ron L | last post by:
All I am working with a DataGrid and a form consisting of a number of text, checkbox, combobox controls, all bound to the same datatable. When I click on my "New" button, I create a new row,...
3
by: Greg | last post by:
My problem is that values in the bool column of a datagrid are only being updated to the database once the focus of the bool cell is lost. This is completely counter-intuitative. When a user clicks...
3
by: Jurgen Appelo | last post by:
I asked this question earlier, but unfortunately the two replies I got did not solve the problem. Here it is again, but now with the code: After an Update my FormView always loses its viewstate...
0
by: Pheasant Plucker® | last post by:
Hi there, Not sure if this is the correct ng and I am not a programmer so excuse me but here goes...:-) We have a custom application that has a hook or calls either the Outlook calendar or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.