简体   繁体   中英

Are triggers asynchronous (on asp.net command object for SQL Server 2008 R2 )

I have some additional (archival) data processing that needs to happen on a SQL Server Update call from ASP.net web app.

I would like to run that independent from the web thread that calls the sql update Command (using the command Object call (not Async) for SQL Server 2008 R2).

All documentation I have read seems to indicate that a SQL Server trigger will not be running asynchronously, meaning that my web thread will be waiting for the update as well as the trigger to finish executing. Can anyone confirm this? Would like to know before I implement an independent async call for my archival sql command.

The trigger is run in the same context and the same transaction as the SQL operation that fired the trigger, eg in the same context and transaction as your UPDATE statement. The transaction (and thus the call from your ASP.NET code) doesn't terminate until the trigger has been executed. That's one of the main reasons why triggers should be used sparingly, and if used, the trigger must be very small, nimble and fast - do NOT do any heavy lifting and extensive processing and external calls in a trigger!

A trigger is part of the transaction that ended up calling the trigger. If you want something to happen outside the transaction, you might want to populate a field (archivestatus, default value 0) that states the record is not archived and then run a job that runs every so often (the timing depends on how many records you need to process and how soon the data needs to be archived) minutes that archives records which have not yet been archived and then updates the field. YOu might also need a trigger to update the record form archived to unarchived status if there are changes to the record.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM