Search code examples
t-sqlasynchronoussql-server-2016service-broker

Asynchronous SQL procedure execution set and wait for completion


Say I have a large set of calls to a procedure to run which have varying parameters but are independent so I want to make parallel/async calls. I use the service broker to fire these all off but the problem I have is I want to know neat ways of knowing how to wait for them all to complete (or error).

Is there a way to do this? I believe I could just loop with waits on the result table checking for completion on that but that isn't very "event triggered". Hoping for a nicer way to do this.

I have used the service broker with queue code and processing based off this other answer: Remus' service broker queuing example


Solution

  • Good day Shiv,

    There are several ways (like always) that you can use in order to implement this requirement. One of these is using this logic:

    (1) Create two queues: one will be the trigger to execute the main SP that you want execute in Asynchronous, and the other will be the trigger to execute whatever you want to execute after all the executions ended.

    (2) When you create the message in the first queue you should also create a message in the second queue, which will only tell us which execution did not ended yet (first queue gives the information which execution started since once we START the execution we use the message and remove it from the queue).

    (3) Inside the SP that you execute using the main first queue (this part executed in synchronous):

    (3.1) execute the queries you need

    (3.2) clear the equivalent message from the second queue (meaning that this message will removed only after the queries ended)

    (3.3) check if there are messages in the second queue. If there are no messages then all the tasks ended and you can execute your final step

    ** Theoretically instead of using the second queue, you can store data in a table, but using second queue should probably give better performance then updating table each time an execution ended. Anyhow, you test the option of using a table as well.