Asynchronous t sql
Upcoming SlideShare
Loading in...5

Asynchronous t sql






Total Views
Views on SlideShare
Embed Views



5 Embeds 42 17 13 9 2 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Asynchronous t sql Asynchronous t sql Presentation Transcript

  • Asynchronous Procedures Reliable T-SQL invocation of procedures using Service Broker Activation Remus Rusanu
  • About Me
    • Consulting on SQL Server problems
    • Founder of
    • Former engineer with SQL Server Engine
    • SQL Server blog at
    • Service Broker Expert
  • Agenda
    • Why asynchronous procedures
    • Service Broker Activation
    • Using Activation for code execution
    • Demo 1
    • Pass parameters to procedures
    • Demo 2
    • Activation context
    • Resources Q&A
  • Why Asynchronous Procedures
    • Allow client to return before processing completes
    • Read operations are suitable for the client stack asynchronous methods: BeginExecuteXXX, EndExecuteXXX
    • Write operations require reliability: must complete even if client disconnects or crashes
  • Reliable Asynchronous Calls
    • The client must trust that once the call is submitted, it is guaranteed to occur
    • No retry logic on client code
    • The call has to be transactional consistent
      • Backup/restore
      • Mirroring/Clustering failover
    • The call must execute even if client disconnects, exits or crashes
  • Service Broker Activation
    • Service Broker: message based distributed application platform
    • Asynchronous Event Driven
    • Activation: start a procedure when there are messages to process
  • Activation internals
    • Each queue is monitored by the system
      • sys.dm_broker_queue_monitors
    • Each time a message is enqueued the queue id is added to the in memory transaction context
    • After commit the transaction notifies all relevant queue monitors that messages have been added
    • The queue monitor will determine if it needs to launch a new activated procedure
  • Activation Triggers After transaction commits During transaction Background connection Client connection EXECUTE AS context Login context When messages available On INSERT/DELETE/UPDATE Batch calls for set processing Each call is standalone Runs in a loop New instance every 5 sec Up to MAX_QUEUE_READERS Runs in each session that invokes it
  • Activation SQL Agent Jobs Internal to SQL Engine External process Agent Runs on demand Runs on timer Consistent backup/restore Backup split with msdb Mirroring failover No failover Available in every edition Not in Express editions For for many small jobs Better fit for few large jobs
  • Using Activation for code execution
    • Recipe:
    • Create a queue with an activated procedure
    • Create a service on the queue
    • SEND a message to the service. Procedure name to be invoked is passed in as the message body.
    • After SEND is committed, the queue procedure will activate
    • Execute the desired procedure from the activated procedure
  • Required Infrastructure
    • Procedure usp_AsyncExecInvoke
      • Enqueues an asynchronous invocation
      • Accepts the procedure to be invoked as parameter
      • Returns an opaque token used to check the invocation result
    • Procedure usp_AsyncExecActivated
      • The activated procedure
      • Must RECEIVE from the queue and execute the desired code
    • Service Broker objects
      • Queue: AsyncExecQueue
      • Service: AsyncExecService
      • Contract, Message Type: use the [DEFAULT] ones
    • Table AsyncExecResults
      • A table to store the execution times and results
  • Asynchronous Execution Workflow
    • Client Session
    • exec usp_AsyncExecInvoke ‘usp_myProcedure’;
    • SEND ‘usp_myProcedure’ to AsyncExecService;
    • INSERT into AsyncExecResults
    • COMMIT
    • SELECT .. FROM AsyncExecResults
    • Background Session
    • Activate usp_AsyncExecActivated
    • RECEIVE from AsyncExecQueue
    • EXEC usp_myProcedure
    • UPDATE AsyncExecResults
    • COMMIT
  • Demo 1
  • Pass Parameters to procedures
    • Pass the parameters with the activation request message as part of the message body
    • Use XML to encode the parameter names an values
    • A generic mechanism is challenging
      • Arbitrary parameter types
      • Arbitrary parameter names
      • Execute the procedure passing in the appropriate parameters requires dynamic SQL
  • Invoke Arbitrary Procedure
    • Original code:
      • exec usp_myProcedure @id=1, @name=‘foo’;
    • Asynchronous Invoke:
      • exec usp_AsyncExecInvoke @procedureName=N’usp_myProcedure’, @p1=1, @n1=N‘@id’, @p2=‘foo’, @n2=N‘@name;
    • @p1, @n1 for first parameter value and name, @p2, @n2 for second parameter value and name etc.
    • @p1, @p2, @p3 etc are of arbitrary types. The formal defintion of usp_AsyncExecInvoke declares them as sql_variant .
  • The Invocation Message
    • < procedure > < name > usp_myProcedure </ name > < parameters > < parameter Name = &quot; @id &quot; BaseType = &quot; numeric &quot; > 1.0 </ parameter > < parameter Name = &quot; @name &quot; BaseType = &quot; nvarchar &quot; > foo </ parameter >
    • … </ parameters > </ procedure >
    • The actual XML also include the numeric precision and varchar/nvarchar length
  • Dynamic SQL for execution
    • declare @pt1 numeric ( 2 , 1 ) declare @pt2 nvarchar ( 6 ) declare @pt3 numeric( 3,1 ) select @pt1 = @x . value ( N'(//procedure/parameters/parameter)[1]' , N'numeric(2,1)' ); select @pt2 = @x . value ( N'(//procedure/parameters/parameter)[2]' , N'nvarchar(6)' ); select @pt3 = @x . value ( N'(//procedure/parameters/parameter)[3]' , N‘numeric(3,1)' ); exec [usp_myProcedure] @id = @pt1 , @name = @pt2 , @amount = @pt3 ;
    • This batch is executed using sp_executesql
    • The message received by the activated procedure is passed as the XML type @x parameter
  • Demo 2
  • Activation context
    • Background Session
      • No connection
      • No Login
    • EXECUTE AS clause is required
    • It is a database impersonation context, not a server impersonation context:
      • Same as EXECUTE AS USER = ‘<username>’;
      • Cannot access any other database
      • Cannot access server level views
      • Extends the database impersonation context to server
      • Same as EXECUTE AS LOGIN = ‘<loginname>’;
    • More secure solution: code signing
  • Resources and Q&A
    • Extending Database Impersonation by Using EXECUTE AS:
    • contact@ rusanu. com
    • @rusanu