Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Why such a delay in the commit? Expand / Collapse
Author
Message
Posted Thursday, April 18, 2013 8:52 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, October 31, 2013 11:03 AM
Points: 2, Visits: 13
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?
Post #1443921
Posted Thursday, April 18, 2013 9:19 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 1:57 AM
Points: 2,729, Visits: 2,646
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
Post #1443942
Posted Thursday, April 18, 2013 9:39 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, April 17, 2014 8:07 AM
Points: 182, Visits: 335
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
Post #1443954
Posted Friday, April 19, 2013 9:51 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 07, 2014 7:35 AM
Points: 1,172, Visits: 2,413
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
Blog: SQLSouth
Twitter: @SQLSouth
Post #1444492
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse