473,943 Members | 15,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetAvailabl eSpotsNum(@Even tID) <= 0
BEGIN
ROLLBACK TRAN
RETURN
END

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

COMMIT TRAN

And function GetAvailableSpo tsNum:

FUNCTION dbo.GetAvailabl eSpotsNum
(
@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 4611
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
1557
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 statements. But somehow ,ONCE IN A WHILE, when executing the stored procedure with about 1000 rows updated, I lost 10-20 records from TABLE_HEAD (seems like 10-20 records were deleted) , and all data rows in TABLE_DETAILS were updated correctly (even...
0
2134
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 property, like below, to show all combobox itens: this.comboBox1.DroppedDown = true;
0
1463
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 encoutered problem. At certain situation the event does not fire when I click my link button. I generate my tree and link button on fly, and associate event with button. Tree is in my top panel. Detail information of each node is in my bottom panel....
4
3369
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 "details" controls are both bound to fields in a dataview called dvItems. The dataview is a subset of a datatable called dtItems, which is simply a copy of all the data in a table called Items. The idea is to allow the user to edit an item in...
14
1765
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, make it the current row, and allow the user to make changes in the form. If the user desires to cancel, they can click the "Cancel Changes" button. Here is where my problems start. Once the Cancel Changes button is clicked, the bindings on the...
3
2296
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 on a bool type cell in a datagrid and sees a check mark put in / removed, then he/she would expect this to be reflected in the databsae immediately. I've even overriden the paint, commit and edit methods of the bool column class (which...
3
3442
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 values. The field values in the FormView are always overwritten by the results of the Update method in the business layer. No matter what I do, the databind always takes place, even when I don't want it to. See the example below. This is a...
0
1855
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 looks in the Inbox for a specific email and acts upon it if found. This has worked great for over a year but when I updated using the usual
0
11537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11131
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7391
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6310
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4512
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3513
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.