Re: Beware: Timestamps are not contained inside Transactions!!

From: Adam Machanic (amachanic_at_hotmail._removetoemail_.com)
Date: 01/20/05


Date: Thu, 20 Jan 2005 17:03:36 -0500

A slightly more performant workaround than selecting the value from the
table might be to get it within the scope of a trigger, from the INSERTED
table... Something like:

use tempdb
go

create table stamp (id int, tstamp timestamp)
go

create trigger returnstamp
on stamp
for insert
as
 select tstamp
 from inserted
go

insert stamp (id)
values (123)
go

drop trigger returnstamp
drop table stamp
go

-- 
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"Dave Slinn" <dslinn@accesscomm.ca> wrote in message
news:OlXh$kz$EHA.2880@TK2MSFTNGP14.phx.gbl...
> Our databases have tables that use a Timestamp column for row-level
> concurrency checking.
>
> All of our Insert and Update stored procedures have an OUTPUT parameter
that
> is supposed to return the value of @@DBTS (which is the last assigned
> Timestamp for the current database).  This value is then passed back into
> the sprocs when a record is to be updated to check if that row has been
> modified by another party between updates.
>
> The PROBLEM is when you have multiple inserts happening at the same time,
it
> is possible that the @@DBTS value does not accurately return the Timestamp
> value for the row just inserted (or updated) in the previous T-SQL batch,
> EVEN IF you have wrapped a BEGIN TRAN and COMMIT TRAN around both the
INSERT
> [record] and SET @returnedTimestamp = @@DBTS statements.
>
> The steps we used to recreate/test this scenario follows:
> 1 - A transaction is opened and a new record is inserted into table A
> 2 - On a separate connection, another transaction is opened and a new
record
> is inserted into table B
> 3 - A call to retrieve the value of @@DBTS and then commit the transaction
> on the first transaction (Table A) returns a value
> 4 - A call to retrieve the value of @@DBTS and then commit the second
> transaction (on Table B) returns the SAME value that was returned in step
3.
>
> Basically, the workaround is to NOT rely on @@DBTS, as it is not
guaranteed
> to the value you were expecting.  Instead we need to perform a SELECT for
> the Timestamp column on the row that was just Inserted or Updated, and
> return that value, as it accurately reflects the Timestamp for that row.
>
>
>


Relevant Pages

  • Re: Beware: Timestamps are not contained inside Transactions!!
    ... table might be to get it within the scope of a trigger, ... create table stamp ... > Our databases have tables that use a Timestamp column for row-level> concurrency checking. ... > The steps we used to recreate/test this scenario follows:> 1 - A transaction is opened and a new record is inserted into table A> 2 - On a separate connection, another transaction is opened and a new record> is inserted into table B ...
    (microsoft.public.sqlserver.programming)
  • Re: Concurrency Help
    ... All you need to do to implement it is to add a TimeStamp ... only client/user X has rights ... Another approach is to use pessimistic locks. ... Then the operation is wrapped inside a transaction. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Combine Secure 3DES Encryption with ability to count occurence of known plaintext - how to accom
    ... >The problem I have is the limited capabilities of the HSP. ... When I have a transaction, ... add, timestamp) to set B. ... Ecould be a deterministic encryption algorithm, or it ...
    (sci.crypt)
  • Re: Query
    ... Why doesn't Informix implement some sort of timestamp type? ... an easier faster mechanism to do this? ... returns current and then calling it from the trigger. ... To locate deletes directly Chandan will need an audit table to ...
    (comp.databases.informix)
  • [PATCH 11/14] IIO: max1363 add software ring buffer support using ring_sw
    ... * to this driver currently ensuring that the timestamp is stored at an 8 ... * occur before we disable the ring buffer (and hence would have no idea ... Some triggers will generate their own time stamp. ... * trigger was set far too high and the reads then locked up the computer. ...
    (Linux-Kernel)