Re: Transactions in SQL Server
From: Todd (Todd_at_%RemoveThis%cprops.com)
Date: 08/06/04
- Next message: gmoney1616: "Sql 2000 high cpu running reports"
- Previous message: Andrew J. Kelly: "Re: Recommended server specification"
- In reply to: Hugo Kornelis: "Re: Transactions in SQL Server"
- Next in thread: Wayne Snyder: "Re: Transactions in SQL Server"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 6 Aug 2004 18:00:03 -0500
Thanks Hugo. I'll try it on Monday and see if I see any difference.
Todd
"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:mru7h059dip0e7k4pr5aelviv8fpv76qi6@4ax.com...
> On Fri, 6 Aug 2004 15:48:40 -0500, Todd wrote:
>
> >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.
> >
>
> Hi Todd,
>
> With the default isolation level, shared locks (used for selecting data)
> are released after executing the SELECT. This can be modified by using a
> different isolation level (repeatable read springs to mind), but that will
> only change the problem, not solve it - you'll now start running into
> deadlocks because both users have a shared lock and want to get an
> exclusive lock to do the update.
>
> You can solve this by using UPDLOCK as locking hint instead of HOLDLOCK.
> The UPDLOCK is used to signal to SQL Server that you intend to modify the
> row you are reading - SQL Server will either grant you an exclusive lock
> or let you wait until other locks on the row are released. I think you
> don't need to change the isolation level, but do test it to be sure!
>
> Best, Hugo
> --
>
> (Remove _NO_ and _SPAM_ to get my e-mail address)
- Next message: gmoney1616: "Sql 2000 high cpu running reports"
- Previous message: Andrew J. Kelly: "Re: Recommended server specification"
- In reply to: Hugo Kornelis: "Re: Transactions in SQL Server"
- Next in thread: Wayne Snyder: "Re: Transactions in SQL Server"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|