Re: Transactions in SQL Server
From: Wayne Snyder (wayne.nospam.snyder_at_mariner-usa.com)
Date: 08/09/04
- Next message: Wayne Snyder: "Re: % Disk Time"
- Previous message: Wayne Snyder: "Re: Start SQLServerAgent job Synchronously"
- In reply to: Todd: "Transactions in SQL Server"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 9 Aug 2004 08:09:18 -0400
What you are doing is called Pessimistic locking. The trouble with
pessimistic locking is that it can reduce concurrency... Make sure you do
some testing the ensure you do not get too much blocking... And make sure
there is an index on appointmentid.
Another method people use (optimistically) is to add a timestamp column to
the table.. This column value is automatically set by SQL and changed each
time there is an update to the row, and it is unique...
You may allow anyone to attempt to change the row, but during the update you
specify that the appointment ID has to match AND the timestamp has to match
the original value. If the timestamps match, no one has changed the row in
the interim.. If the timestamps do not match, then someone else has changed
the row since THIS person first started using it...
-- Wayne Snyder, MCDBA, SQL Server MVP Mariner, Charlotte, NC www.mariner-usa.com (Please respond only to the newsgroups.) I support the Professional Association of SQL Server (PASS) and it's community of SQL Server professionals. www.sqlpass.org "Todd" <Todd@%RemoveThis%cprops.com> wrote in message news:%23Va6Sc$eEHA.904@TK2MSFTNGP09.phx.gbl... > I am having some SQL Server 2000 transactional issues and was wondering if > anyone knew of some settings I need to have to prevent the following. I have > a table that is called the appointments table. One of its fields is called > "in_edit_by" which is used to notify the calling program that someone has a > record open and is editing it. Right now, I have a the following process > wrapped in a transaction: > > 1: Read in_edit_by into a temp variable > 2: If in_edit_by is not null (ie, no one is editing the record) update the > table and set in_edit_by = current user > 3: Do a select of the table and tell the calling program the result can be > edited. > (If item 2 is not editable, it will still read the data, but send a flag > back that the data is not editable) > 4) Close the transaction > > Here's the actual TSQL: > > > BEGIN TRANSACTION trn_RetrieveAppt > > -- Mark the appointment Locked first > SET @LockedBy = (SELECT in_edit_by FROM appointment HOLDLOCK > WHERE appointment_id = @ApptID ) > IF @LockedBy is NULL or @LockedBy = @User > BEGIN > UPDATE appointment > SET in_edit_by = @User, > ApptOpenMaintDate = getdate() > WHERE appointment_id = @ApptID > SET @isEditable = 'T' > END > ELSE > BEGIN > SET @isEditable = 'F' > > END > > --Read a bunch of stuff from this table and others and pass back to the > calling program including the @isEditable flag. > COMMIT TRANSACTION trn_RetrieveAppt > > It works for most everything, except it seems to fail when both users run > the query next to simultaneiously (down to less than 1 second difference) -- > in this case, even though wrapped in transaction, BOTH user A and B get a > NULL value into their temp variable which has really bad consequences > because the application then thinks both User A and B have editable records. > It is as if, even though I specified the transaction to start at the top, it > really doens't start until I begin the UPDATE. I'm wondering if this is > because of the SET TRANSACTION ISOLATION LEVEL default behavior? If so, what > should I pick? Or can I set a ROWLOCK on the initial select? It's really > tough to duplicate here -- it seems to happen in the field where there's > lots of users accessing simulataneously. > >
- Next message: Wayne Snyder: "Re: % Disk Time"
- Previous message: Wayne Snyder: "Re: Start SQLServerAgent job Synchronously"
- In reply to: Todd: "Transactions in SQL Server"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|