Why such a delay in the commit?

  • I have a scheduled job (.Net, WinOS Task Scheduler) which completes shortly after it's called but the SQL work it should accomplish in real time does not happen until several hours later.

    The .Net scheduled job calls a stored procedure which updates 2 "top level" tables and outputs 3 data readers (that's what they are to the .Net program). Both updated tables have 2 update triggers each (one trigger inserts to a log table & one manages some computed fields in "quick search" tables). One of the fields updated in each table is a datetime field - set to getdate() within the sproc. Temp tables are used to identify the rows to be updated. Quick pseudo-code (showing just one update and select statement):

    ----

    declare @mydate dateime

    set @mydate = getdate()

    declare @UpdateIDs table(MyID int)

    insert @UpdateIDs

    select CaseID

    from TopLevelTable1

    where ...

    begin transaction

    update TopLevelTable1

    set Flag1 = [new value]

    , Flag1Date = @mydate

    where CaseID in (select MyID from @UpdateIDs)

    -- 2 triggers are fired in conjunction with this statement

    commit transaction

    select [several fields]

    from @UpdateIDs t

    inner join [several joins from temp table]

    ----

    The problem: the .Net program runs at 11p each night and completes in about 20 seconds. It sends out an email for each row in the select and one to me regarding the ending status. All those emails happen at 11p. The job reports success.

    But the time stamp (the param @mydate & the field TopLevelTable1.Flag1Date) is 6am the next day. The trigger inserting to TopLevelTable1_Log has a field LogDate which is set to getdate() within the trigger and its value is also 6am.

    There is no other maintenance work being done at that time. Transaction logs (using sqllogship.exe) are being done and full database backup is done at 1:45am. That full backup has the unmodified rows relating to the this job.

    What could be happening?

  • Hi,

    We had similar problem recently. Do you have the option "with recompile" in your stored procedure? We've resolved it by attaching that option to the sp.

    Regards

    IgorMi

    Igor Micev,My blog: www.igormicev.com

  • hi,

    I belive there is error within transaction and transaction is not committed. can you add error handling within the transaction to check if there is any.

    Alternately, add test line just after commit to check if opentransactions are there. update some flag in either case to know transaction status. Hope this will narrow down to the reason.

    btw, is the trigger updating the same table? if yes, what is nested triggers setting?

    Seraj Alam

  • pbarbee (4/18/2013)


    I have a scheduled job (.Net, WinOS Task Scheduler) which completes shortly after it's called but the SQL work it should accomplish in real time does not happen until several hours later.

    The .Net scheduled job calls a stored procedure which updates 2 "top level" tables and outputs 3 data readers (that's what they are to the .Net program). Both updated tables have 2 update triggers each (one trigger inserts to a log table & one manages some computed fields in "quick search" tables). One of the fields updated in each table is a datetime field - set to getdate() within the sproc. Temp tables are used to identify the rows to be updated. Quick pseudo-code (showing just one update and select statement):

    ----

    declare @mydate dateime

    set @mydate = getdate()

    declare @UpdateIDs table(MyID int)

    insert @UpdateIDs

    select CaseID

    from TopLevelTable1

    where ...

    begin transaction

    update TopLevelTable1

    set Flag1 = [new value]

    , Flag1Date = @mydate

    where CaseID in (select MyID from @UpdateIDs)

    -- 2 triggers are fired in conjunction with this statement

    commit transaction

    select [several fields]

    from @UpdateIDs t

    inner join [several joins from temp table]

    ----

    The problem: the .Net program runs at 11p each night and completes in about 20 seconds. It sends out an email for each row in the select and one to me regarding the ending status. All those emails happen at 11p. The job reports success.

    But the time stamp (the param @mydate & the field TopLevelTable1.Flag1Date) is 6am the next day. The trigger inserting to TopLevelTable1_Log has a field LogDate which is set to getdate() within the trigger and its value is also 6am.

    There is no other maintenance work being done at that time. Transaction logs (using sqllogship.exe) are being done and full database backup is done at 1:45am. That full backup has the unmodified rows relating to the this job.

    What could be happening?

    Have you confirmed that your time zone settings are consistent across servers? SQL Server gets the getdate() value from the OS of the server on which it runs. If your mail server has a different time zone setting, the time of the e-mails will be offset from the getdate() value in SQL Server. If all the code you describe runs in the same batch or on the same connection, you won't get any data back from the SELECT at the end until the INSERT and UPDATE statements are committed, so they must be finished when the e-mail process gets the rows from the SELECT.

    P.S. - You would see the offsets you describe if your mail server uses the local time for either the US Mountain or US Pacific time zone and your SQL Server uses UTC time (depending on daylight savings settings).

    Jason Wolfkill

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply