<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>rusanu.com</title>
	<atom:link href="http://rusanu.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://rusanu.com</link>
	<description>RUSANU CONSULTING LLC</description>
	<pubDate>Mon, 25 Aug 2008 08:28:32 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Certificate Not Yet Valid</title>
		<link>http://rusanu.com/2008/08/25/certificate-not-yet-valid/</link>
		<comments>http://rusanu.com/2008/08/25/certificate-not-yet-valid/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 08:28:32 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Troubleshooting]]></category>

		<guid isPermaLink="false">http://rusanu.com/?p=107</guid>
		<description><![CDATA[Copyright Remus Rusanu 2008. Visit the original article at http://rusanu.com/2008/08/25/certificate-not-yet-valid/&#160;&#160;So you are running into a blocking issue, try to find a solution and you gets back a reply like &#8216;Sorry, I cannot repro your problem&#8217;. You go back and try again, and sure the problem is still there. You dig deeper, go to the Advanced [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2008. Visit the original article at <a href="http://rusanu.com/2008/08/25/certificate-not-yet-valid/">http://rusanu.com/2008/08/25/certificate-not-yet-valid/</a></p>&nbsp;&nbsp;<br><p>So you are running into a blocking issue, try to find a solution and you gets back a reply like &#8216;Sorry, I cannot repro your problem&#8217;. You go back and try again, and sure the problem is still there. You dig deeper, go to the Advanced search options on Google, but still cannot find the answer. Desperately, you try again and, surprise, the problem has vanished. Sounds familiar?</p>
<p>I know of one such problem that appears again and again when discussing Service Broker, Database Mirroring and, to a lesser extent, SQL Server 2005 Cryptographic facilities. I want to show you what the problem is, what&#8217;s causing it and even drill a bit into how something so basic has slipped into the product.</p>
<p><span id="more-107"></span></p>
<h1>Certificate Not Yet Valid</h1>
<p>Lets do a very simple example of a Service Broker conversation that uses a certificate. First create a test database, then create a service that we will use for our test:</p>
<pre><span style="color: Black"></span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">master</span><span style="color:Black"> </span><span style="color:Blue">key</span><span style="color:Black">
	</span><span style="color:Blue">encryption</span><span style="color:Black"> </span><span style="color:Blue">by</span><span style="color:Black"> </span><span style="color:Blue">password</span><span style="color:Black"> </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Red">'Password#123'</span><span style="color:Gray">;
</span><span style="color:Black">go

</span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">certificate</span><span style="color:Black"> test </span><span style="color:Blue">with</span><span style="color:Black"> </span><span style="color:Blue">subject</span><span style="color:Black"> </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Red">'test'</span><span style="color:Gray">;
</span><span style="color:Black">go

</span><span style="color:Blue">backup</span><span style="color:Black"> </span><span style="color:Blue">certificate</span><span style="color:Black"> test </span><span style="color:Blue">to</span><span style="color:Black"> </span><span style="color:Blue">file</span><span style="color:Black"> </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Red">'test.cer'</span><span style="color:Gray">;
</span><span style="color:Black">go

</span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">queue</span><span style="color:Black"> testQueue</span><span style="color:Gray">;
</span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">service</span><span style="color:Black"> testService
	</span><span style="color:Blue">on</span><span style="color:Black"> </span><span style="color:Blue">queue</span><span style="color:Black"> testQueue </span><span style="color:Gray">(</span><span style="color:Black">[DEFAULT]</span><span style="color:Gray">);
</span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">remote</span><span style="color:Black"> </span><span style="color:Blue">service</span><span style="color:Black"> </span><span style="color:Blue">binding</span><span style="color:Black"> testBinding
	</span><span style="color:Blue">to</span><span style="color:Black"> </span><span style="color:Blue">service</span><span style="color:Black"> </span><span style="color:Red">'testService'</span><span style="color:Black"> </span><span style="color:Blue">with</span><span style="color:Black"> </span><span style="color:Blue">user</span><span style="color:Black"> </span><span style="color:Gray">=</span><span style="color:Black"> [dbo]</span><span style="color:Gray">;
</span><span style="color:Black">go</span>
</pre>
<p>This setup allows us to send a message on a conversation from service <span style="color:Green">testService</span> to service <span style="color:Green">testService</span>:</p>
<pre><span style="color: Black"></span><span style="color:Blue">declare</span><span style="color:Black"> @h </span><span style="color:Blue">uniqueidentifier</span><span style="color:Gray">;
</span><span style="color:Blue">begin</span><span style="color:Black"> </span><span style="color:Blue">dialog</span><span style="color:Black"> </span><span style="color:Blue">conversation</span><span style="color:Black"> @h
	</span><span style="color:Blue">from</span><span style="color:Black"> </span><span style="color:Blue">service</span><span style="color:Black"> [testService]
	</span><span style="color:Blue">to</span><span style="color:Black"> </span><span style="color:Blue">service</span><span style="color:Black"> </span><span style="color:Red">'testService'</span><span style="color:Gray">;
</span><span style="color:Blue">send</span><span style="color:Black"> </span><span style="color:Blue">on</span><span style="color:Black"> </span><span style="color:Blue">conversation</span><span style="color:Black"> @h</span><span style="color:Gray">;
</span><span style="color:Black">go</span>
</pre>
<p>And now check the <span style="color:Green">sys.transmission_queue</span>:</p>
<pre><span style="color: Black">
</span><span style="color:Blue">waitfor</span><span style="color:Black"> </span><span style="color:Blue">delay</span><span style="color:Black"> </span><span style="color:Red">'00:00:05'</span><span style="color:Gray">;
</span><span style="color:Blue">select</span><span style="color:Black"> transmission_status </span><span style="color:Blue">from</span><span style="color:Black"> </span><span style="color:Green">sys.transmission_queue</span><span style="color:Gray">;
</span><span style="color:Black">go</span>
</pre>
<p>And you will see this message: <span style="color:Red">The security certificate bound to database principal (Id: 1) is not yet valid. Either wait for the certificate to become valid or install a certificate that is currently valid</span>. Or you won&#8217;t see anything. Wait! Is there an error or isn&#8217;t there one? Well.. it depends on where you live. And it also depends whether is summer or winter outside.</p>
<h1>Universal Time</h1>
<p>Any sort of communication between machines must rely on <a href="http://en.wikipedia.org/wiki/Universal_Time" target="_blank">UTC</a>, because machines can be distributed across the globe and have different local times. Using UTC timestamps solves this issue. This also applies to certificates. The fields <span style="color:Green">Validity/Not Before</span> and <span style="color:Green">Validity/Not After</span> must be expressed in UTC so that the certificate issued on one part of the globe is safely used on another part of the globe. As I write this post from Bucharest that is on the GMT+2 time zone and summer daylight saving time is in effect, my local time is 3 hours ahead of the UTC time. I can test this from SQL:</p>
<pre><span style="color: Black"></span><span style="color:Blue">select</span><span style="color:Black"> </span><span style="color:Fuchsia">getutcdate</span><span style="color:Gray">(),</span><span style="color:Black"> </span><span style="color:Fuchsia">getdate</span><span style="color:Gray">()</span></pre>
<p>I get back the values <tt>2008-08-25 09:25:16</tt> (my local time) and <tt>2008-08-25 06:25:16</tt> (UTC time). When I created the certificate I did not specify a the <tt>START_DATE</tt> nor the <tt>EXPIRY_DATE</tt> values, so the certificate was created using the creation moment as <span style="color:Green">Validity/Not Before</span> and creation moment + 1 year as <span style="color:Green">Validity/Not After</span>, which are the X.509 fields corresponding to <tt>START_DATE</tt> and <tt>EXPIRY_DATE</tt>. Is there some way to verify this? Yes, we can export the certificate and then open it in the system certificate viewer:</p>
<pre><span style="color: Black"></span><span style="color:Blue">backup</span><span style="color:Black"> </span><span style="color:Blue">certificate</span><span style="color:Black"> test </span><span style="color:Blue">to</span><span style="color:Black"> </span><span style="color:Blue">file</span><span style="color:Black"> </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Red">'test.cer'</span><span style="color:Gray">;
</span><span style="color:Black">go</span></pre>
<p>The <tt>test.cer</tt> file was saved in the same location your <tt>master</tt> files are. Double-click this file and you get to see all the details of the certificate, and we can check the Valid from:</p>
<div class="post-image"><a href="http://rusanu.com/wp-content/uploads/2008/08/certificate.png" target="_blank"><img src="http://rusanu.com/wp-content/uploads/2008/08/certificate.png" alt="Certificate Viewer" title="Click on the image for a full size view" width="250"/></a></div>
<p>But you can see that the time displayed is &#8230; <tt>Monday, August 25, 2008 12:25:19 PM</tt>. That is not the 9:25 AM reported by <span style="color:Fuchsia">GETDATE()</span> neither the 6:25 AM reported by <span style="color:Fuchsia">GETUTCDATE()</span>. OK, so my computer local time is 3 hours ahead of UTC and the Certificate Viewer displays the times in local time, that means that the certificate has it&#8217;s UTC time stamp at 9:25. Isn&#8217;t this wrong? It sure is. And in fact that is a SQL Server 2005 bug. When creating a new certificate, it stamps the certificate valid and expiration dates with the local time values instead of the UTC time values. However, when it uses the certificate the time fields are used correctly, so the time value in the field is interpreted as a UTC time value. This is why the certificate I created at 9:25 AM is not valid until 12:25 PM the same day!. And this is because I did the test in Bucharest during summer. If I&#8217;d repeat the same test in Moscow in winter, the certificate would not be valid for 3 hours. In London during summer is not valid one hour. In Mumbai would be 5 hours and 30 minutes. As I said, the result depends on where you&#8217;re doing the test and also whether daylight saving time is in effect or not!</p>
<p>So why some of you did not see any problem? Because if you live on the Western Hemisphere and you are <i>behind</i> the UTC clock, then the certificate is valid immediately as you create it. In fact, is it stamped as being valid few hours in the past, depending on your time zone.</p>
<h1>Redmond, WA, USA</h1>
<p>So when you post your problem on the forums, somebody from the SQL team is looking at it, trying to repro, and doesn&#8217;t see any problem. Of course, they are located in Redmond so they don&#8217;t see the problem :). Or perhaps an MVP or a forum moderator is trying to help you. Again, if they are US based (and many are), they would not repro the issue, since their local time is safe from this problem.</p>
<p>Is this problem still in SQL Server 2005? I tested on build 9.00.3042 which is SP2, and the problem is present. How could Microsoft not be aware of this issue when they released the product? Very simple: the entire test infrastructure is running on Redmond time! In fact the problem was reported early by users: <a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=125262" target="_blank">https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=125262</a> but was closed as not repro. Of course, the person who tried to repro was based on Redmond, where the issue does not repro. Other people are still running into this issue today, look at <a href="http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3779091&#038;SiteID=1" target="_blank">this</a> post on the MSDN forums, or <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1046928&#038;SiteID=1" target="_blank">here</a>.</p>
<h1>Workaround</h1>
<p>How do you resolve the issue? Very simply, specify an explicit <tt>START_DATE</tt> when you create the certificate:</p>
<pre><span style="color: Black"></span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">certificate</span><span style="color:Black"> test
	</span><span style="color:Blue">with</span><span style="color:Black"> </span><span style="color:Blue">subject</span><span style="color:Black"> </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Red">'test'</span><span style="color:Gray">,
</span><span style="color:Black">	</span><span style="color:Blue">start_date</span><span style="color:Black"> </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Red">'2008-08-24'</span><span style="color:Gray">;
</span></pre>
<p>Why does the problem seem to sometimes magically fix itself when you try again later? Because after the number of hours that is the difference between your local time and the UTC time has passed, the certificate is valid.</p>
<p>And one last note: what start date is displayed in the certificate meta data view?</p>
<pre><span style="color: Black"></span><span style="color:Blue">select</span><span style="color:Black"> </span><span style="color:Gray">*</span><span style="color:Black"> </span><span style="color:Blue">from</span><span style="color:Black"> </span><span style="color:Green">sys.certificates</span><span style="color:Gray">
</span></pre>
<p>Yes indeed, that is <tt>2008-08-25 09:25:19.000</tt>. Confusing? You bet. Correct? Not a chance&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2008/08/25/certificate-not-yet-valid/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Error Handling and Activation</title>
		<link>http://rusanu.com/2008/08/13/error-handling-and-activation/</link>
		<comments>http://rusanu.com/2008/08/13/error-handling-and-activation/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 18:44:59 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Troubleshooting]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://rusanu.com/?p=103</guid>
		<description><![CDATA[Copyright Remus Rusanu 2008. Visit the original article at http://rusanu.com/2008/08/13/error-handling-and-activation/&#160;&#160;
I have previously talked here about the queue monitors and the role they play in launching activated procedures. If you recall, I&#8217;ve mentioned that the Queue Monitors will enter the NOTIFIED state after they launch a stored procedure and not launch again the procedure until the [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2008. Visit the original article at <a href="http://rusanu.com/2008/08/13/error-handling-and-activation/">http://rusanu.com/2008/08/13/error-handling-and-activation/</a></p>&nbsp;&nbsp;<br><p />
<p>I have previously talked <a href="http://rusanu.com/2008/08/03/understanding-queue-monitors/" target="_blank">here</a> about the queue monitors and the role they play in launching activated procedures. If you recall, I&#8217;ve mentioned that the Queue Monitors will enter the <span style="color:Green">NOTIFIED</span> state after they launch a stored procedure and not launch again the procedure until the Queue Monitor notices that <span style="color:Blue">RECEIVE</span> statements are being issued against the queue. In an older <a href="http://rusanu.com/2007/10/31/error-handling-in-service-broker-procedures/" target="_blank">post</a> I have also talked about how difficult is to get error handling right, and in particular cast and convention errors. This may seem a trivial problem but in the Service Broker programs it is actually a serious problem because of the frequent conversation to and from XML. These two separate issues can actually &#8216;cooperate&#8217; into a somehow surprising behavior. Namely if the activated procedure hits an error <i>before</i> it completes the <span style="color:Blue">RECEIVE</span> statement, the Queue Monitor will stay in <span style="color:Green">NOTIFIED</span> state and won&#8217;t activate again the procedure. Although this looks similar to the typical poison message behavior when the queue is automatically disabled, this is a different issue. And unlike the poison message case, in the case when the Queue Monitor is stranded in <span style="color:Green">NOTIFIED</span> state you can <b>not</b> get a notification that the queue is no longer functional.</p>
<p>This post continues with an example showing how a relatively safe activated procedure can end up in this situation.</p>
<p><span id="more-103"></span></p>
<p>Lets consider what happens when a typical Service Broker activated procedure is presented with an invalid XML fragment in a message. First, lets create a pair of services:</p>
<pre><span style="color: Black"></span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">queue</span><span style="color:Black"> [initiator]</span><span style="color:Gray">;
</span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">service</span><span style="color:Black"> [http://rusanu.com/2008/08/13/initiator] </span><span style="color:Blue">on</span><span style="color:Black"> </span><span style="color:Blue">queue</span><span style="color:Black"> [initiator]</span><span style="color:Gray">;
</span><span style="color:Black">go

</span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">queue</span><span style="color:Black"> [target]</span><span style="color:Gray">;
</span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">service</span><span style="color:Black"> [http://rusanu.com/2008/08/13/target]
	</span><span style="color:Blue">on</span><span style="color:Black"> </span><span style="color:Blue">queue</span><span style="color:Black"> [target]
	</span><span style="color:Gray">(</span><span style="color:Black">[DEFAULT]</span><span style="color:Gray">);
</span><span style="color:Black">go</span></pre>
<p>Lets say the target service is used to log user logins from workstations. We have a table that keeps these login events:</p>
<pre><span style="color: Black"></span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">table</span><span style="color:Black"> [user_log] </span><span style="color:Gray">(
</span><span style="color:Black">	[id] </span><span style="color:Blue">int</span><span style="color:Black"> </span><span style="color:Gray">not</span><span style="color:Black"> </span><span style="color:Gray">null</span><span style="color:Black"> </span><span style="color:Blue">identity</span><span style="color:Black"> </span><span style="color:Gray">(</span><span style="color:Black">1</span><span style="color:Gray">,</span><span style="color:Black">1</span><span style="color:Gray">)</span><span style="color:Black"> </span><span style="color:Blue">primary</span><span style="color:Black"> </span><span style="color:Blue">key</span><span style="color:Gray">,
</span><span style="color:Black">	[user_name] </span><span style="color:Blue">varchar</span><span style="color:Gray">(</span><span style="color:Black">256</span><span style="color:Gray">),
</span><span style="color:Black">	[workstation] </span><span style="color:Blue">varchar</span><span style="color:Gray">(</span><span style="color:Black">256</span><span style="color:Gray">),
</span><span style="color:Black">	[login_time] </span><span style="color:Blue">datetime</span><span style="color:Black"> </span><span style="color:Gray">not</span><span style="color:Black"> </span><span style="color:Gray">null);
</span><span style="color:Black">go</span></pre>
<p>The initiator service is sending XML messages that embed the user, workstation and time for a login event:</p>
<pre><span style="color:Gray">&lt;login_info xmlns="//rusanu.com/2008/08/13" time="2008-08-13T08:00:00"&gt;
  &lt;user&gt;John Doe&lt;/user&gt;
  &lt;workstation&gt;PCLAB36X&lt;/workstation&gt;
&lt;/login_info&gt;
</span></pre>
<p>So we create an activated procedure that is extracting the name, workstation and time from the XML and inserts the information into the [user_log] table:</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">create</span><span style="color:Black"> </span><span style="color:Blue">procedure</span><span style="color:Black"> usp_target
</span><span style="color:Blue">as
begin
</span><span style="color:Black">	</span><span style="color:Blue">set</span><span style="color:Black"> </span><span style="color:Blue">nocount</span><span style="color:Black"> </span><span style="color:Blue">on</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">declare</span><span style="color:Black"> @h </span><span style="color:Blue">uniqueidentifier</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">declare</span><span style="color:Black"> @mt </span><span style="color:Blue">sysname</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">declare</span><span style="color:Black"> @msg </span><span style="color:Blue">xml</span><span style="color:Gray">;

</span><span style="color:Black">	</span><span style="color:Blue">begin</span><span style="color:Black"> </span><span style="color:Blue">try
</span><span style="color:Black">		</span><span style="color:Blue">begin</span><span style="color:Black"> </span><span style="color:Blue">transaction
</span><span style="color:Black">
		</span><span style="color:Blue">waitfor</span><span style="color:Black"> </span><span style="color:Gray">(</span><span style="color:Blue">receive</span><span style="color:Black"> </span><span style="color:Blue">top</span><span style="color:Gray">(</span><span style="color:Black">1</span><span style="color:Gray">)</span><span style="color:Black">
			@h </span><span style="color:Gray">=</span><span style="color:Black"> [conversation_handle]</span><span style="color:Gray">,
</span><span style="color:Black">			@mt </span><span style="color:Gray">=</span><span style="color:Black"> [message_type_name]</span><span style="color:Gray">,
</span><span style="color:Black">			@msg </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Fuchsia">cast</span><span style="color:Gray">(</span><span style="color:Black">message_body </span><span style="color:Blue">as</span><span style="color:Black"> </span><span style="color:Blue">xml</span><span style="color:Gray">)
</span><span style="color:Black">			</span><span style="color:Blue">from</span><span style="color:Black"> [target]</span><span style="color:Gray">),</span><span style="color:Black"> </span><span style="color:Blue">timeout</span><span style="color:Black"> 1000</span><span style="color:Gray">;
</span><span style="color:Black">
		</span><span style="color:Blue">if</span><span style="color:Black"> </span><span style="color:Gray">(</span><span style="color:Black">@h </span><span style="color:Gray">is</span><span style="color:Black"> </span><span style="color:Gray">not</span><span style="color:Black"> </span><span style="color:Gray">null)
</span><span style="color:Black">		</span><span style="color:Blue">begin
</span><span style="color:Black">			</span><span style="color:Blue">if</span><span style="color:Black"> </span><span style="color:Gray">(</span><span style="color:Black">@mt </span><span style="color:Gray">=</span><span style="color:Black"> N</span><span style="color:Red">'DEFAULT'</span><span style="color:Gray">)
</span><span style="color:Black">			</span><span style="color:Blue">begin
</span><span style="color:Black">				</span><span style="color:Blue">with</span><span style="color:Black"> </span><span style="color:Blue">xmlnamespaces</span><span style="color:Black"> </span><span style="color:Gray">(</span><span style="color:Blue">DEFAULT</span><span style="color:Black"> </span><span style="color:Red">'//rusanu.com/2008/08/13'</span><span style="color:Gray">)
</span><span style="color:Black">				</span><span style="color:Blue">insert</span><span style="color:Black"> </span><span style="color:Blue">into</span><span style="color:Black"> [user_log] </span><span style="color:Gray">(
</span><span style="color:Black">					[user_name]</span><span style="color:Gray">,
</span><span style="color:Black">					[workstation]</span><span style="color:Gray">,
</span><span style="color:Black">					[login_time]</span><span style="color:Gray">)
</span><span style="color:Black">				</span><span style="color:Blue">select</span><span style="color:Black"> T</span><span style="color:Gray">.</span><span style="color:Black">c</span><span style="color:Gray">.</span><span style="color:Black">value</span><span style="color:Gray">(</span><span style="color:Red">'user[1]'</span><span style="color:Gray">,</span><span style="color:Black"> </span><span style="color:Red">'varchar(256)'</span><span style="color:Gray">),</span><span style="color:Black">
					T</span><span style="color:Gray">.</span><span style="color:Black">c</span><span style="color:Gray">.</span><span style="color:Black">value</span><span style="color:Gray">(</span><span style="color:Red">'workstation[1]'</span><span style="color:Gray">,</span><span style="color:Black"> </span><span style="color:Red">'varchar(256)'</span><span style="color:Gray">),
</span><span style="color:Black">					T</span><span style="color:Gray">.</span><span style="color:Black">c</span><span style="color:Gray">.</span><span style="color:Black">value</span><span style="color:Gray">(</span><span style="color:Red">'@time'</span><span style="color:Gray">,</span><span style="color:Black"> </span><span style="color:Red">'datetime'</span><span style="color:Gray">)
</span><span style="color:Black">					</span><span style="color:Blue">from</span><span style="color:Black"> @msg</span><span style="color:Gray">.</span><span style="color:Black">nodes</span><span style="color:Gray">(</span><span style="color:Red">'/login_info'</span><span style="color:Gray">)</span><span style="color:Black"> T</span><span style="color:Gray">(</span><span style="color:Black">c</span><span style="color:Gray">);
</span><span style="color:Black">			</span><span style="color:Blue">end
</span><span style="color:Black">
			</span><span style="color:Blue">end</span><span style="color:Black"> </span><span style="color:Blue">conversation</span><span style="color:Black"> @h</span><span style="color:Gray">;
</span><span style="color:Black">		</span><span style="color:Blue">end</span><span style="color:Black">
		</span><span style="color:Blue">commit</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">end</span><span style="color:Black"> </span><span style="color:Blue">try
</span><span style="color:Black">	</span><span style="color:Blue">begin</span><span style="color:Black"> </span><span style="color:Blue">catch
</span><span style="color:Black">		</span><span style="color:Blue">declare</span><span style="color:Black"> @error </span><span style="color:Blue">int</span><span style="color:Gray">,</span><span style="color:Black"> @message </span><span style="color:Blue">varchar</span><span style="color:Gray">(</span><span style="color:Black">4000</span><span style="color:Gray">),</span><span style="color:Black"> @xstate </span><span style="color:Blue">int</span><span style="color:Gray">;
</span><span style="color:Black">		</span><span style="color:Blue">select</span><span style="color:Black"> @error </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Fuchsia">ERROR_NUMBER</span><span style="color:Gray">(),</span><span style="color:Black"> @message </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Fuchsia">ERROR_MESSAGE</span><span style="color:Gray">(),</span><span style="color:Black"> @xstate </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Fuchsia">XACT_STATE</span><span style="color:Gray">();
</span><span style="color:Black">		</span><span style="color:Blue">if</span><span style="color:Black"> @xstate </span><span style="color:Gray">!=</span><span style="color:Black"> 0
			</span><span style="color:Blue">rollback</span><span style="color:Gray">;
</span><span style="color:Black">
		</span><span style="color:Green">----------------------------
</span><span style="color:Black">		</span><span style="color:Green">-- Error logging goes here
</span><span style="color:Black">		</span><span style="color:Green">----------------------------
</span><span style="color:Black">		</span><span style="color:Blue">print</span><span style="color:Black"> @message</span><span style="color:Gray">;
</span><span style="color:Black">
	</span><span style="color:Blue">end</span><span style="color:Black"> </span><span style="color:Blue">catch</span><span style="color:Black">
</span><span style="color:Blue">end
</span><span style="color:Black">go</span>

<span style="color:Blue">alter</span><span style="color:Black"> </span><span style="color:Blue">queue</span><span style="color:Black"> [target] </span><span style="color:Blue">with</span><span style="color:Black"> activation </span><span style="color:Gray">(
</span><span style="color:Black">	</span><span style="color:Blue">status</span><span style="color:Black"> </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Blue">on</span><span style="color:Gray">,
</span><span style="color:Black">	procedure_name </span><span style="color:Gray">=</span><span style="color:Black"> [usp_target]</span><span style="color:Gray">,
</span><span style="color:Black">	</span><span style="color:Blue">execute</span><span style="color:Black"> </span><span style="color:Blue">as</span><span style="color:Black"> </span><span style="color:Blue">owner</span><span style="color:Gray">,
</span><span style="color:Black">	max_queue_readers </span><span style="color:Gray">=</span><span style="color:Black"> 1</span><span style="color:Gray">);
</span><span style="color:Black">go</span></pre>
<p>So we go ahead and test the procedure, by sending a message that will activate the procedure:</p>
<pre><span style="color:Blue">declare</span><span style="color:Black"> @h </span><span style="color:Blue">uniqueidentifier</span><span style="color:Gray">;
</span><span style="color:Blue">declare</span><span style="color:Black"> @msg </span><span style="color:Blue">nvarchar</span><span style="color:Gray">(</span><span style="color:Fuchsia">max</span><span style="color:Gray">);
</span><span style="color:Blue">select</span><span style="color:Black"> @msg </span><span style="color:Gray">=</span><span style="color:Black"> N</span><span style="color:Red">'&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;login_info xmlns="//rusanu.com/2008/08/13" time="2008-08-13T08:00:00.000"&gt;
  &lt;user&gt;John Doe&lt;/user&gt;
  &lt;workstation&gt;PCLAB36X&lt;/workstation&gt;
&lt;/login_info&gt;'</span><span style="color:Gray">;
</span><span style="color:Blue">begin</span><span style="color:Black"> </span><span style="color:Blue">dialog</span><span style="color:Black"> </span><span style="color:Blue">conversation</span><span style="color:Black"> @h
	</span><span style="color:Blue">from</span><span style="color:Black"> </span><span style="color:Blue">service</span><span style="color:Black"> [http://rusanu.com/2008/08/13/initiator]
	</span><span style="color:Blue">to</span><span style="color:Black"> </span><span style="color:Blue">service</span><span style="color:Black"> N</span><span style="color:Red">'http://rusanu.com/2008/08/13/target'
</span><span style="color:Black">	</span><span style="color:Blue">with</span><span style="color:Black"> </span><span style="color:Blue">encryption</span><span style="color:Black"> </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Blue">off</span><span style="color:Gray">;
</span><span style="color:Blue">send</span><span style="color:Black"> </span><span style="color:Blue">on</span><span style="color:Black"> </span><span style="color:Blue">conversation</span><span style="color:Black"> @h </span><span style="color:Gray">(</span><span style="color:Black">@msg</span><span style="color:Gray">);
</span><span style="color:Black">go</span></pre>
<p>And when we check the [user_log] table, nothing is there. How come? Well, we did send an invalid XML message. Because we assigned the XML to an NVARCHAR variable, it is stored as Unicode encoding. But our XML fragment declares itself as UTF-8 encoding, hence it is invalid and casting it to XML datatype will fail. This sort of XML errors are less obvious and I&#8217;ve seen in real life situations encoding mixed up like this.</p>
<p>If we check the server ERRORLOG or the system Event Log (eventvwr.exe) we&#8217;ll find the error message from the XML convertion:</p>
<p><tt>The activated proc [dbo].[usp_target] running on queue test.dbo.target output the following:  &#8216;XML parsing: line 1, character 38, unable to switch the encoding&#8217;</tt></p>
<p>This error happens <i>during</i> the <span style="color:Blue">RECEIVE</span> statement. This interrupts the statement <i>before</i> the Queue Monitor is notified that RECEIVEs are occurring, and thus the activated procedure exists but the Queue Monitor is left in <span style="color:Green">NOTIFIED</span> state. We can check this in the DMV <span style="color:Green">sys.dm_broker_queue_monitors</span>. Although the queue is active, it refuses to activate again the procedure.</p>
<p>The workaround for this is failry easy: simply move the cast <b>after</b> the <span style="color:Blue">RECEIVE</span> statement:</p>
<pre><span style="color:Blue">alter</span><span style="color:Black"> </span><span style="color:Blue">procedure</span><span style="color:Black"> usp_target
</span><span style="color:Blue">as
begin
</span><span style="color:Black">	</span><span style="color:Blue">set</span><span style="color:Black"> </span><span style="color:Blue">nocount</span><span style="color:Black"> </span><span style="color:Blue">on</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">declare</span><span style="color:Black"> @h </span><span style="color:Blue">uniqueidentifier</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">declare</span><span style="color:Black"> @mt </span><span style="color:Blue">sysname</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">declare</span><span style="color:Black"> @msg </span><span style="color:Blue">xml</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">declare</span><span style="color:Black"> @msg_bin </span><span style="color:Blue">varbinary</span><span style="color:Gray">(</span><span style="color:Fuchsia">max</span><span style="color:Gray">);

</span><span style="color:Black">	</span><span style="color:Blue">begin</span><span style="color:Black"> </span><span style="color:Blue">try
</span><span style="color:Black">		</span><span style="color:Blue">begin</span><span style="color:Black"> </span><span style="color:Blue">transaction
</span><span style="color:Black">
		</span><span style="color:Blue">waitfor</span><span style="color:Black"> </span><span style="color:Gray">(</span><span style="color:Blue">receive</span><span style="color:Black"> </span><span style="color:Blue">top</span><span style="color:Gray">(</span><span style="color:Black">1</span><span style="color:Gray">)</span><span style="color:Black">
			@h </span><span style="color:Gray">=</span><span style="color:Black"> [conversation_handle]</span><span style="color:Gray">,
</span><span style="color:Black">			@mt </span><span style="color:Gray">=</span><span style="color:Black"> [message_type_name]</span><span style="color:Gray">,
</span><span style="color:Black">			@msg_bin </span><span style="color:Gray">=</span><span style="color:Black"> [message_body]
			</span><span style="color:Blue">from</span><span style="color:Black"> [target]</span><span style="color:Gray">),</span><span style="color:Black"> </span><span style="color:Blue">timeout</span><span style="color:Black"> 1000</span><span style="color:Gray">;
</span><span style="color:Black">
		</span><span style="color:Blue">if</span><span style="color:Black"> </span><span style="color:Gray">(</span><span style="color:Black">@h </span><span style="color:Gray">is</span><span style="color:Black"> </span><span style="color:Gray">not</span><span style="color:Black"> </span><span style="color:Gray">null)
</span><span style="color:Black">		</span><span style="color:Blue">begin
</span><span style="color:Black">			</span><span style="color:Blue">if</span><span style="color:Black"> </span><span style="color:Gray">(</span><span style="color:Black">@mt </span><span style="color:Gray">=</span><span style="color:Black"> N</span><span style="color:Red">'DEFAULT'</span><span style="color:Gray">)
</span><span style="color:Black">			</span><span style="color:Blue">begin
</span><span style="color:Black">				</span><span style="color:Blue">select</span><span style="color:Black"> @msg </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Fuchsia">cast</span><span style="color:Gray">(</span><span style="color:Black">@msg_bin </span><span style="color:Blue">as</span><span style="color:Black"> </span><span style="color:Blue">xml</span><span style="color:Gray">);
</span><span style="color:Black">				</span><span style="color:Blue">with</span><span style="color:Black"> </span><span style="color:Blue">xmlnamespaces</span><span style="color:Black"> </span><span style="color:Gray">(</span><span style="color:Blue">DEFAULT</span><span style="color:Black"> </span><span style="color:Red">'//rusanu.com/2008/08/13'</span><span style="color:Gray">)
</span><span style="color:Black">				</span><span style="color:Blue">insert</span><span style="color:Black"> </span><span style="color:Blue">into</span><span style="color:Black"> [user_log] </span><span style="color:Gray">(
</span><span style="color:Black">					[user_name]</span><span style="color:Gray">,
</span><span style="color:Black">					[workstation]</span><span style="color:Gray">,
</span><span style="color:Black">					[login_time]</span><span style="color:Gray">)
</span><span style="color:Black">				</span><span style="color:Blue">select</span><span style="color:Black"> T</span><span style="color:Gray">.</span><span style="color:Black">c</span><span style="color:Gray">.</span><span style="color:Black">value</span><span style="color:Gray">(</span><span style="color:Red">'user[1]'</span><span style="color:Gray">,</span><span style="color:Black"> </span><span style="color:Red">'varchar(256)'</span><span style="color:Gray">),</span><span style="color:Black">
					T</span><span style="color:Gray">.</span><span style="color:Black">c</span><span style="color:Gray">.</span><span style="color:Black">value</span><span style="color:Gray">(</span><span style="color:Red">'workstation[1]'</span><span style="color:Gray">,</span><span style="color:Black"> </span><span style="color:Red">'varchar(256)'</span><span style="color:Gray">),
</span><span style="color:Black">					T</span><span style="color:Gray">.</span><span style="color:Black">c</span><span style="color:Gray">.</span><span style="color:Black">value</span><span style="color:Gray">(</span><span style="color:Red">'@time'</span><span style="color:Gray">,</span><span style="color:Black"> </span><span style="color:Red">'datetime'</span><span style="color:Gray">)
</span><span style="color:Black">					</span><span style="color:Blue">from</span><span style="color:Black"> @msg</span><span style="color:Gray">.</span><span style="color:Black">nodes</span><span style="color:Gray">(</span><span style="color:Red">'/login_info'</span><span style="color:Gray">)</span><span style="color:Black"> T</span><span style="color:Gray">(</span><span style="color:Black">c</span><span style="color:Gray">);
</span><span style="color:Black">			</span><span style="color:Blue">end
</span><span style="color:Black">
			</span><span style="color:Blue">end</span><span style="color:Black"> </span><span style="color:Blue">conversation</span><span style="color:Black"> @h</span><span style="color:Gray">;
</span><span style="color:Black">		</span><span style="color:Blue">end</span><span style="color:Black">
		</span><span style="color:Blue">commit</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">end</span><span style="color:Black"> </span><span style="color:Blue">try
</span><span style="color:Black">	</span><span style="color:Blue">begin</span><span style="color:Black"> </span><span style="color:Blue">catch
</span><span style="color:Black">		</span><span style="color:Blue">declare</span><span style="color:Black"> @error </span><span style="color:Blue">int</span><span style="color:Gray">,</span><span style="color:Black"> @message </span><span style="color:Blue">varchar</span><span style="color:Gray">(</span><span style="color:Black">4000</span><span style="color:Gray">),</span><span style="color:Black"> @xstate </span><span style="color:Blue">int</span><span style="color:Gray">;
</span><span style="color:Black">		</span><span style="color:Blue">select</span><span style="color:Black"> @error </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Fuchsia">ERROR_NUMBER</span><span style="color:Gray">(),</span><span style="color:Black"> @message </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Fuchsia">ERROR_MESSAGE</span><span style="color:Gray">(),</span><span style="color:Black"> @xstate </span><span style="color:Gray">=</span><span style="color:Black"> </span><span style="color:Fuchsia">XACT_STATE</span><span style="color:Gray">();
</span><span style="color:Black">		</span><span style="color:Blue">if</span><span style="color:Black"> @xstate </span><span style="color:Gray">!=</span><span style="color:Black"> 0
			</span><span style="color:Blue">rollback</span><span style="color:Gray">;
</span><span style="color:Black">
		</span><span style="color:Green">----------------------------
</span><span style="color:Black">		</span><span style="color:Green">-- Error logging goes here
</span><span style="color:Black">		</span><span style="color:Green">----------------------------
</span><span style="color:Black">		</span><span style="color:Blue">print</span><span style="color:Black"> @message</span><span style="color:Gray">;
</span><span style="color:Black">
	</span><span style="color:Blue">end</span><span style="color:Black"> </span><span style="color:Blue">catch</span><span style="color:Black">
</span><span style="color:Blue">end
</span><span style="color:Black">go
</span></pre>
<p>With this modified procedure the conversation error is causing the <span style="color:Blue">RECEIVE</span> to rollback, but the Queue Monitor has already entered the <span style="color:Green">RECEIVES_OCCURRING</span> state and will activate the procedure again. Five consecutive rollbacks are triggering the poison message detection mechanism and the queue is disabled as expected. Why is this a better behavior than before? Because you can have monitoring set up around the BROKER_QUEUE_DISABLED event notification and intervene: remove the poison message and re-activate the queue.</p>
<h3>XML message validation</h3>
<p>In my example I intentionally used the DEFAULT message type, with no validation, and did not use a message type with well_formed_xml message validation. The later would had muted the whole point of the errors occurring during XML cast since and invalid message would not event be enqueued int the [target] queue. Indeed, using message type validation is the best option for such a case, but none the less, I have seen situations like the one I&#8217;ve described in production. It can happen when the developers intentionally omit the XML message type validation in the message type declaration to gain a bit of performance, or because they consider themselves safe from an invalid XML ever being sent.</p>
<h2>Conclusion</h2>
<p>Always make sure your <span style="color:Blue">RECEIVE</span> statement is being executed in your activated procedure. If you are doing any cast, is better to receive into intermediate types that do not require a cast and only perform the cast <i>after</i> the <span style="color:Blue">RECEIVE</span> statement has finished. Otherwise you may end up with your service not activating but your notification infrastructure never detecting this problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2008/08/13/error-handling-and-activation/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Service Broker Team Blog</title>
		<link>http://rusanu.com/2008/08/08/service-broker-team-blog/</link>
		<comments>http://rusanu.com/2008/08/08/service-broker-team-blog/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 18:50:09 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://rusanu.com/?p=100</guid>
		<description><![CDATA[Copyright Remus Rusanu 2008. Visit the original article at http://rusanu.com/2008/08/08/service-broker-team-blog/&#160;&#160;In case you miss it, the SQL Server Service Broker has recently started posting in an MSDN team blog: http://blogs.msdn.com/sql_service_broker. The team members have already made strides and covered some hot topics like shipping near real-time OLTP data to ETL, some advanced discussion of SSB routing, [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2008. Visit the original article at <a href="http://rusanu.com/2008/08/08/service-broker-team-blog/">http://rusanu.com/2008/08/08/service-broker-team-blog/</a></p>&nbsp;&nbsp;<br><p>In case you miss it, the SQL Server Service Broker has recently started posting in an MSDN team blog: <a href="http://blogs.msdn.com/sql_service_broker">http://blogs.msdn.com/sql_service_broker</a>. The team members have already made strides and covered some hot topics like <a href="http://blogs.msdn.com/sql_service_broker/archive/2008/07/09/real-time-data-integration-with-service-broker-and-other-sql-techniques.aspx">shipping near real-time OLTP data to ETL</a>, some <a href="http://blogs.msdn.com/sql_service_broker/archive/2008/07/14/using-multiple-routes-in-service-broker.aspx">advanced discussion of SSB routing</a>, <a href="http://blogs.msdn.com/sql_service_broker/archive/2008/06/30/poison-message-handling.aspx">poison message handling</a>, some juicy <a href="http://blogs.msdn.com/sql_service_broker/archive/2008/06/26/service-broker-periodic-tasks.aspx">internals how-it-works</a> details and also revisited that never exhausted topic of <a href="http://blogs.msdn.com/sql_service_broker/archive/2008/06/10/a-simple-secure-dialog-with-certificates.aspx">setting up your first remote dialog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2008/08/08/service-broker-team-blog/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Database Journal articles on Service Broker</title>
		<link>http://rusanu.com/2008/08/05/database-journal-articles-on-service-broker/</link>
		<comments>http://rusanu.com/2008/08/05/database-journal-articles-on-service-broker/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 13:31:07 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://rusanu.com/2008/08/05/database-journal-articles-on-service-broker/</guid>
		<description><![CDATA[Copyright Remus Rusanu 2008. Visit the original article at http://rusanu.com/2008/08/05/database-journal-articles-on-service-broker/&#160;&#160;Marcin Policht has a series of articles on databasejournal.com covering Service Broker. So far the series has covered an introduction, a  presentation of SSB objects and a conversation primer.
These series are part of a SQL Express cover done by Database Journal and they are to [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2008. Visit the original article at <a href="http://rusanu.com/2008/08/05/database-journal-articles-on-service-broker/">http://rusanu.com/2008/08/05/database-journal-articles-on-service-broker/</a></p>&nbsp;&nbsp;<br><p><a href="http://www.databasejournal.com/article.php/1503191" target="_blank">Marcin Policht</a> has a series of articles on <a href="http://www.databasejournal.com" target="_blank">databasejournal.com</a> covering Service Broker. So far the series has covered an <a href="http://www.databasejournal.com/features/mssql/article.php/3757581" target="_blank">introduction</a>, a  <a href="http://www.databasejournal.com/features/mssql/article.php/3759311" target="_blank">presentation of SSB objects</a> and a <a href="http://www.databasejournal.com/features/mssql/article.php/3762261" target="_blank">conversation primer</a>.</p>
<p>These series are part of a SQL Express cover done by Database Journal and they are to continue with more SSB goodies.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2008/08/05/database-journal-articles-on-service-broker/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Understanding Queue Monitors</title>
		<link>http://rusanu.com/2008/08/03/understanding-queue-monitors/</link>
		<comments>http://rusanu.com/2008/08/03/understanding-queue-monitors/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 17:58:04 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Troubleshooting]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://rusanu.com/2008/08/03/understanding-queue-monitors/</guid>
		<description><![CDATA[Copyright Remus Rusanu 2008. Visit the original article at http://rusanu.com/2008/08/03/understanding-queue-monitors/&#160;&#160;A major class of Service Broker applications have nothing to do with the distributed application capabilities SSB was designed for. Instead they leverage SSB&#8217;s capability to run a stored procedure outside the context of the user connection. This capability enables to schedule execution and don&#8217;t wait [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2008. Visit the original article at <a href="http://rusanu.com/2008/08/03/understanding-queue-monitors/">http://rusanu.com/2008/08/03/understanding-queue-monitors/</a></p>&nbsp;&nbsp;<br><p>A major class of Service Broker applications have nothing to do with the distributed application capabilities SSB was designed for. Instead they leverage SSB&#8217;s capability to run a stored procedure outside the context of the user connection. This capability enables to schedule execution and don&#8217;t wait for it to finish, or to schedule multiple procedures to execute in paralel. Often the developers that use these capabilities don&#8217;t care for the reliable message delivery SSB offers and many see the complex infrastructure of message types, contracts and services as a mere hurdle. Nonetheless sooner or later they have to troubleshoot an issue and then is when many of you find my blog and my articles. Troubleshooting activated procedure is not difficult, but one simply has to know where to look and what to look for.</p>
<p>In this entry I want to shed some light in the soul that drives the activation process: the Queue Monitors.</p>
<p><span id="more-97"></span></p>
<p>A Queue Monitor is a state machine that resides in the SQL Server and decides when to launch a new instance of activated procedure into execution. The <code><span style="color: green">sys.dm_broker_queue_monitors</span></code> DMV is used to view the Queue Monitors:</p>
<div class="post-image"><a href="http://rusanu.com/wp-content/uploads/2008/08/dm_broker_queue_monitors.png" target="_blank"><img src="http://rusanu.com/wp-content/uploads/2008/08/dm_broker_queue_monitors.thumbnail.png" alt="select * from sys.dm_broker_queue_monitors" title="Click on the image for a full size view" /></a></div>
<p>This DMV is described in BOL at <a href="http://msdn.microsoft.com/en-us/library/ms177628.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms177628.aspx</a> where the columns types and decriptions can be found.</p>
<p>There is a Queue Monitor for each queue with activation enabled in the SQL Server instance. The DMV does not contain the database name nor the queue name, but you can join with <code><span style="color: green">sys.service_queues</span></code> on queue_id to get the queue names. Note that even when you run this query on a brand new install of SQL Server you will get two Queue Monitors: <code>InternalMailQueue</code> and <code>ExternalMailQueue</code>. These are the queues placed in <code>msdb</code> used by the Database Mail feature of SQL Server.</p>
<h2>Queue Monitor States</h2>
<p>To start troublesooting an activation problem you need to understand the Queue Monitor states:</p>
<ul>
<li><code><span style="color: Green">INACTIVE</span></code>: there are no messages available to be received in the monitored queue.</li>
<li><code><span style="color: Green">NOTIFIED</span></code>: messages are available and the activated procedure was launched into execution.</li>
<li><code><span style="color: Green">RECEIVES_OCCURING</span></code>: the activated procedure is issuing <code><span style="color: Blue">RECEIVE</span></code> command against the monitored queue.</li>
</ul>
<p>The important thing to notice here is that the Queue Monitor will go into <code><span style="color: Green">NOTIFIED</span></code> state when an activated procedure was launched and will not launch again any procedure until a <code><span style="color:Blue">RECEIVE</span></code> statement is issued against the monitored queue. This is done in order to prevent situations when a queue is incorrectly configured and launches the wrong stored procedure into execution. Altough in practice is the activated procedure that is expected to execute the <code><span style="color: Blue">RECEIVE</span></code> statement, this is not enforced by the Queue Monitor. Any connection that executes a <code><span style="color: Blue">RECEIVE</span></code> while the Queue Monitor is in <code><span style="color: Green">NOTIFIED</span></code> state will cause the Queue Monitor to enter the <code><span style="color: Green">RECEIVES_OCCURING</span></code> state.</p>
<p>This activation state machine also handles sending the <code>QUEUE_ACTIVATION</code> events for <a href="http://msdn.microsoft.com/en-us/library/ms171581.aspx" target="_blank">Event-Based Activation</a>. The only difference that instead of launching a stored procedure into execution the Queue Monitor sends a message of type <code><span style="color:Green">http://schemas.microsoft.com/SQL/Notifications/EventNotification</span></code> to the notification subscriber. Again, same as with internal activation, once the notification is sent the Queue Monitor enters the <code><span style="color: Green">NOTIFIED</span></code> state and will not send more notifications messages until a <code><span style="color: Blue">RECEIVE</span></code> statement is executed on the monitored queue.</p>
<p>The Queue Monitor will go back to <code><span style="color: Green">INACTIVE</span></code> state when there are no more messages ready to be received in the queue.</p>
<h2>Queue Monitor Timers</h2>
<p>The Queue Monitors are also responsible for handling the activation ramp up for handling a higher load by launching more instances of the activated procedure. Once the Queue Monitor enters the <code><span style="color: Green">RECEIVES_OCCURING</span></code> state (ie. a <code><span style="color:Blue">RECEIVE</span></code> statement is issued) it will start an internal timer. Each time a <code><span style="color:Blue">RECEIVE</span></code> statement on the monitored queue returns an empty rowset this timer is reset. If this timer ever reaches a threshold then a new instance of the activated procedure is launched. The reasoning behind this mechanism is that activated procedures are supposed to issue <code><span style="color:Blue">RECEIVE</span></code> statements and process the received messages. If <code><span style="color:Blue">RECEIVE</span></code> does not find any available messages and returns an empty rowset the activation timer is reset because it means that there are no messages available for receive, so there is no reason to launch a new instance of the procedure. The timer threshold is 5 seconds and currently is hardcoded.</p>
<p>This timers are also used for sending new <code>QUEUE_ACTIVATION</code> event messages if the Event-Based Activation is used instead of internal activation.</p>
<h2>Troubleshooting</h2>
<p>If you find yourself looking at Queue Monitors in <code><span style="color: Green">sys.dm_broker_queue_monitors</span></code> is usualy because of one reason: your activated procedure is failing to launch.</p>
<p>If there is no record for your queue in <code><span style="color: Green">sys.dm_broker_queue_monitors</span></code> then this indicates that the Service Broker activation is not even monitoring the queue. For activation to monitor the queue several settings have to be properly configured:</p>
<p></p>
<ul>
<li>The queue has to be enabled. Check <code>is_receive_enabled</code> and <code>is enqueue_enabled</code> in <code><span style="color: green">sys.service_queues</span></code>.</li>
<li>Activation has to be enabled on the queue. Check <code>is_activation_enabled</code> in <code><span style="color: green">sys.service_queues</span></code>.</li>
<li>Activation has to be properly configured. Check <code>activation_procedure</code>, <code>max_queue_readers</code> and <code>execute_ss_principal_id</code> in <code><span style="color: green">sys.service_queues</span></code>.</li>
<li>The Service Broker in the database has to be enabled. Check <code>is_broker_enabled</code> in <code><span style="color: green">sys.databases</span></code>.</li>
</ul>
<p></p>
<p>If there is a record for your queue in <code><span style="color: Green">sys.dm_broker_queue_monitors</span></code> and its state is <code><span style="color: Green">INACTIVE</span></code> then there are no messages in the queue ready to be received. Check the content of the queue with a <code><span style="color:Blue">SELECT</span>&nbsp;*&nbsp;<span style="color:Blue">FROM</span>&nbsp;<span style="color:Green">[&lt;your_queue_name&gt;]</span></code>. Messages ready to be received have to be unlocked and the status has to be 1 <code><span style="color:Green">ready</span></code>. Note that the BOL documentation of <a href="http://msdn.microsoft.com/en-us/library/ms186963.aspx" target="_blank">RECEIVE</a> erroneously shows the state 0 as <code><span style="color:Green">ready</span></code>, which is incorrect.</p>
<p>If the Queue Monitor for your queue is in <a name="hint"></a><code><span style="color: Green">NOTIFIED</span></code> and it stays in that state is an indication that your activated procedure is not issuing the correct <code><span style="color:Blue">RECEIVE</span></code> statement. Check your activated procedure. Note that if you change the procedure the Queue Monitor will <b>not</b> detect this change and launch again the procedure. The easiest thing to do is to recycle the Queue Monitor by disabling and then enabling activation on the queue:<br/><br />
    <code><br />
<span style="color: Blue">ALTER QUEUE</span> [<span style="color: Green">&lt;your_queue&gt;</span>] <span style="color: Blue">WITH ACTIVATION (STATUS = OFF)</span>;<br/><br />
<span style="color: Blue">ALTER QUEUE</span> [<span style="color: Green">&lt;your_queue&gt;</span>] <span style="color: Blue">WITH ACTIVATION (STATUS = ON)</span>;</code>
</p>
<p>Finally if the Queue Monitor for your queue is in the state <span style="color: Green">RECEIVES_OCCURING</span> then it indicates that the <code><span style="color:Blue">RECEIVE</span></code> statement is being issued against the queue. Make sure your assesment that the procedure is not being launched is correct. Check if there are other applications or procedures that are erroneously receiving from your queue.</p>
<h2>Demo Caution</h2>
<p>I will end with a cautionary tale. I was at a presentation where the speaker was showing a very nice monitoring facility he had built around the event notifications for DDL that he was using to monitor a farm of SQL Server instances. As he was making his way through the demo, he showed how it attaches a stored procedure to the queue to monitor for the notification messages. But in order to not perturb the presentation flow (at moment focused on setting up activation) he did <i>not</i> create the whole procedure, just a stub for it. Something like this:<br />
<code><br />
<span style="color: Blue">CREATE PROCEDURE</span>&nbsp;usp_NotificationProcessor<br/><br />
<span style="color: Blue">AS<br />
BEGIN</span><br />
	<span style="color:Green">&#8211;TODO: add code here</span><br/><br />
<span style="color: Blue">END</span>;<br />
</code>
</p>
<p>The speaker then continued with his demo and attached the procedure to the queue for activation. Now since he had already created the subscription for all DDL events on the demo database, his queue was receiving the notification messaged as he was creating and altering objects in the database.</p>
<p>He finaly reaches the point when he changes the stub procedure to actually process the notifications and then the audience eagerly awaits the results. And nothing happens.</p>
<p>Can you guess what was wrong? Go back and read <a href="#hint">this</a> for a hint.</p>
<p>Since activation was enabled and there were messages to received, the Queue Monitor has launched the procedure altough it was nothing but a stub. The Queue Monitor has entered the <code><span style="color: Green">NOTIFIED</span></code> state and would not launch again the procedure until a <code><span style="color:Blue">RECEIVE</span></code> was issued. Later when the stub procedure was changed to do the actual processing, the procedure would not be launched by the Queue Monitor because it would still wait for <code><span style="color:Blue">RECEIVE</span></code>. This can be easily fixed by turning activation on the queue off and back on, as I showed before.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2008/08/03/understanding-queue-monitors/feed/</wfw:commentRss>
		</item>
		<item>
		<title>OT: Terrarium 2 released in the wild</title>
		<link>http://rusanu.com/2008/07/16/ot-terrarium-2-released-in-the-wild/</link>
		<comments>http://rusanu.com/2008/07/16/ot-terrarium-2-released-in-the-wild/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 16:24:19 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://rusanu.com/2008/07/16/ot-terrarium-2-released-in-the-wild/</guid>
		<description><![CDATA[Copyright Remus Rusanu 2008. Visit the original article at http://rusanu.com/2008/07/16/ot-terrarium-2-released-in-the-wild/&#160;&#160;Those who played with the original Terrarium project back in .Net 1.1 now have a chance to revisit this fun project. The Terrarium2 for .Net 2.0 was released on codeplex, including the project sources.
If you never had a chance to see the Terrarium, check it out. [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2008. Visit the original article at <a href="http://rusanu.com/2008/07/16/ot-terrarium-2-released-in-the-wild/">http://rusanu.com/2008/07/16/ot-terrarium-2-released-in-the-wild/</a></p>&nbsp;&nbsp;<br><p>Those who played with the original Terrarium project back in .Net 1.1 now have a chance to revisit this fun project. The Terrarium2 for .Net 2.0 was released on <a href="http://www.codeplex.com/terrarium2">codeplex</a>, including the project sources.<br />
If you never had a chance to see the Terrarium, check it out. Terrarium allows you to create creatures with behavior you program in .Net and set them up into a virtual peer-to-peer world of Terrarium instances. You can track your creature in the wild, how does it do against other creatures. A nice incentive to learn some basic AI stuff and keep your programming skills sharp in a fun virtual fight-to-the-death competition with other programmers.<br />
Read more on the <a href="http://blogs.msdn.com/windowssdk/archive/2008/07/16/net-terrarium-2-0-source-code-now-available.aspx">Windows SDK team blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2008/07/16/ot-terrarium-2-released-in-the-wild/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dialog Security Configuration Wizard in Toad</title>
		<link>http://rusanu.com/2008/05/22/dialog-security-configuration-wizard-in-toad/</link>
		<comments>http://rusanu.com/2008/05/22/dialog-security-configuration-wizard-in-toad/#comments</comments>
		<pubDate>Thu, 22 May 2008 22:22:48 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://rusanu.com/2008/05/22/dialog-security-configuration-wizard-in-toad/</guid>
		<description><![CDATA[Copyright Remus Rusanu 2008. Visit the original article at http://rusanu.com/2008/05/22/dialog-security-configuration-wizard-in-toad/&#160;&#160;On my last post I&#8217;ve said that the Toad tools from Quest lack the option to configure Dialog Security for Service Broker. I stand corrected, as they actualy do. Not only that, but they support both full security and anonymous security, and the configuration is also [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2008. Visit the original article at <a href="http://rusanu.com/2008/05/22/dialog-security-configuration-wizard-in-toad/">http://rusanu.com/2008/05/22/dialog-security-configuration-wizard-in-toad/</a></p>&nbsp;&nbsp;<br><p>On my <a href="http://rusanu.com/2008/05/16/viable-tools-for-service-broker/" target="_blank">last post</a> I&#8217;ve said that the Toad tools from Quest lack the option to configure Dialog Security for Service Broker. I stand corrected, as they actualy do. Not only that, but they support both <a href="http://technet.microsoft.com/en-us/library/ms166036.aspx" target="_blank">full security and anonymous security</a>, and the configuration is also done through a wizard. The Dialog Security Configuration Wizard is launched from the context menu of a service.</p>
<p>The wizard is explicit for security and does not combine security and routing, like the <a href="http://ww.codeplex.com/slm" target="_blank">Service Listing Manager</a> does. So to fully configure a pair of services you have to run the Service Broker Application Wizard first and then to follow up with the Dialog Security Configuration Wizard.</p>
<p>The Dialog Security Wizard will guide you through the steps of creating the certificates, set up a database master key, create the users needed, exchange the certificates and set up the remote service binding.</p>
<p><span id="more-94"></span></p>
<div class="post-image"><a href='http://rusanu.com/wp-content/uploads/2008/05/dialogsecurity.PNG' target="_blank"><img src='http://rusanu.com/wp-content/uploads/2008/05/dialogsecurity.PNG' alt="Dialog Security Wizard" width="250" title="Click on the image for a full size view"/></a></div>
<p>My one comment on this Wizard is that it somehow contradicts the Broker Aplication Wizard. The Service Broker Application Wizard automatically grants SEND permission to [public] on the target service. In practice this allows any sender to send message to this service.  Even if later the Dialog Security Wizard is run, the [public] role is still allowed to SEND on the target service. This is irelevant from a functional point of view, the two services configured through the Wizard will work as expected, but from a security/surface area point of view the service is exposed to a potential attack.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2008/05/22/dialog-security-configuration-wizard-in-toad/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Viable tools for Service Broker</title>
		<link>http://rusanu.com/2008/05/16/viable-tools-for-service-broker/</link>
		<comments>http://rusanu.com/2008/05/16/viable-tools-for-service-broker/#comments</comments>
		<pubDate>Fri, 16 May 2008 10:05:39 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://rusanu.com/2008/05/16/viable-tools-for-service-broker/</guid>
		<description><![CDATA[Copyright Remus Rusanu 2008. Visit the original article at http://rusanu.com/2008/05/16/viable-tools-for-service-broker/&#160;&#160;
Technically Service Broker can offer a huge value over any other comparable technology, but every project faces in the early stages the same barrier: the extremely steep learning curve faced by developers and dba in order deploy a Service Broker solution. As the adopters try to [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2008. Visit the original article at <a href="http://rusanu.com/2008/05/16/viable-tools-for-service-broker/">http://rusanu.com/2008/05/16/viable-tools-for-service-broker/</a></p>&nbsp;&nbsp;<br><p>
Technically Service Broker can offer a huge value over any other comparable technology, but every project faces in the early stages the same barrier: the extremely steep learning curve faced by developers and dba in order deploy a Service Broker solution. As the adopters try to set up a simple scenario to &#8216;play&#8217; with the technology before making a decision they are facing an apparently insurmountable challenge: set up the configuration for that very first message to get through. Not only services, queue, contracts and message types have to be correctly configured, but also the transport security (endpoints and certificates, logins and endpoint connect permissions), service routing and last but not least service security (remote service bindings, again certificates, users without login and service send permission). These are all new concepts, not trivial ones, and they <strong><em>all</em></strong> have to be correct. And I haven&#8217;t even mentioned database master keys! So one is faced with the task of configuring more than ten new object types, never encountered before, just to test his first message. No wonder some feel frustrated and may abandon the technology before even seeing what is capable of.
</p>
<p>
Tools are the biggest missing piece of this puzzle. A wizard that would walk the new developer or dba through the maze of new concepts and objects to help him get that very first test scenario to work would be tremendous help. Unfortunately the set of tools that ship with SQL Server is in dire need of such wonder. For years my recommendation was to give a try to the <a href="http://www.codeplex.com/slm" target="_blank">Service Listing Manager</a> tool. This tool, specially the command line version, takes all the headake out and can do all the work for you, configuring everything needed for two services to be able to exchange messages. But this tool is not an officially supported Microsoft release, and therefore many organizations cannot afford to use it in their environment.</p>
<p>
Recently I have learned that 3rd party vendors stepped up to the challenge and have come up with a set of tools designed for Service Broker. Quest Software&#8217;s <a href="http://www.quest.com/toad-for-sql-server/" target="_blank">Toad</a> is a well known toolset, and on Oracle environment is <strong><em>the</em></strong> toolset, the one everybody uses. And now it contains the Service Broker Manager, a set of tools that covers everything related to Service Broker in SQL Server 2005. Organizations that are looking for a set of tools for Service Broker deployment, maintenance and administration have now a commercially supported alternative.
</p>
<p><span id="more-84"></span></p>
<p>
The <a href="http://www.quest.com/images/popup.asp?path=/toad-for-sql-server/images/ToadSS30ServiceBroker.jpg&#038;width=1024&#038;height=710" target="_blank"> Service Broker Manager</a> tools for Toad are available on all versions (Professional, Xpert and Dev suite), according to this <a href="http://www.quest.com/toad-for-sql-server/configurations-checklist.aspx" target="_blank">comparison chart</a>. After you connect to a SQL Server instance from the Connection Manager you get the option to launch the Service Broker Manager in the tools menu:</p>
<div class="post-image"><a href='http://rusanu.com/wp-content/uploads/2008/05/servicebroker-menu.PNG' target="_blank"><img src='http://rusanu.com/wp-content/uploads/2008/05/servicebroker-menu.PNG' alt="Service Broker Manager in Tools menu" width="450" title="Click on the image for a full size view"/></a></div>
<p>
The Service Broker Manager has a typical Object Explorer style tool, with a hierarchical tree view of all databases that are enabled for Service Broker and all broker related objects in each database.</p>
<div class="post-image"><a href='http://rusanu.com/wp-content/uploads/2008/05/servicebrokermanager-full.PNG' target="_blank"><img src='http://rusanu.com/wp-content/uploads/2008/05/servicebrokermanager-full.PNG' alt="Service Broker Manager Full view" width="450" title="Click on the image for a full size view" /></a></div>
<p>
One thing I like about the Toad model of the Object Explorer is that it has a brief description for each object type definition. If you are new to Service Broker and have never encountered before a Queue, you can click the Description tab and read the definition of a Queue, similar to what you would find in Books On Line on the same subject. The description tab also contains quick action links for the context of the object type, like a &#8216;Create Queue&#8217; link. While the quick link action idea is nice, I find a bit confusing its placement on the &#8216;Definition&#8217; tab.</p>
<p>
As expected the context menu of each object in this tree view has options to view properties, create, modify and drop each object type. Interestingly the hierarchy of objects includes all the expected Service Broker types like services, queue and contract, but also object that are not &#8216;owned&#8217; by Service Broker, but are indeed heavily used by it, like certificates and XML Schemas. I found this very interesting, that Quest has considered putting the object where they are used and needed as opposed to where they &#8216;officially&#8217; belong in the Microsoft blessed taxonomy of objects in the database. For instance the SQL Server Management Studio shows the Certificates in the &#8216;Security&#8217; node because they are a Security feature and the XML Schemas in the Programmability/Types/XML Schema Collections node, because they are an XML data type feature. Just like supermarkets place tomato sauce right next to pasta on the shelves, Quest has placed the objects where they are needed, not in the parent node of the team that owns the feature. Of course there are other tools in Toad like the Database Browser tool that are showing the Certificates where they &#8216;properly&#8217; belong in the Security tab, since Certificates truly are a &#8216;Security&#8217; feature and are used in code signing, encryption and other strictly security related roles.</p>
<p>
The Service Broker Manager has functionalities that cover all the broker needs, including configuring endpoints, enabling and disabling broker in the database and even setting the trustworthy bit on or off on the database:</p>
<div class="post-image"><a href='http://rusanu.com/wp-content/uploads/2008/05/databaseproperties.PNG' target="_blank"><img src='http://rusanu.com/wp-content/uploads/2008/05/databaseproperties.PNG' alt="Service Broker Database properties" title="Click on the image for a full size view" width="450"/></a></div>
<p>
Another nice feature of the Service Broker Manager is the fact that it shows conversation endpoints, messages in queues, messages for each conversation and, last but not least, the database transmission queue. The current SQL Server Management Studio for SQL Server 2005 doesn&#8217;t have anything similar. The SQL Server 2008 SSMS has the Broker Statistics Report that also shows the conversations and messages as well as the transmission queue. But the Toad one goes not one, but two steps further. For one it allows you to actually Edit conversations:</p>
<div class="post-image"><a href='http://rusanu.com/wp-content/uploads/2008/05/conversationproperties.PNG' target="_blank"><img src='http://rusanu.com/wp-content/uploads/2008/05/conversationproperties.PNG' alt="Conversation Properties" title="Click on the image for a full size view" width="450" /></a></div>
<p>The only &#8216;editable&#8217; properties on a conversation are it&#8217;s conversation group and its timer. I reckon I was very surprised to see these two properties as editable. I was so used to see them as T-SQL statements (<a href="http://msdn.microsoft.com/en-us/library/ms174987.aspx" target="_blank">MOVE CONVERSATION</a> and <a href="http://msdn.microsoft.com/en-us/library/ms187804.aspx" target="_blank">BEGIN CONVERSATION TIMER</a>) that I have never thought about them as means to &#8216;edit&#8217; a conversation property. Is true that they may appear so for an administrator looking at a conversation, but they serve clear application programming roles related to <a href="http://msdn2.microsoft.com/en-us/library/ms171615.aspx" target="_blank">correlated messages locking</a> (the MOVE CONVERSATION) or to retry mechanisms and time outs (the BEGIN CONVERSATION TIMER).</p>
<p>
And the second &#8216;further step&#8217; Toad offers in regard to conversations is that it actually has options to begin and end conversations and even send a message:</p>
<div class="post-image"><a href='http://rusanu.com/wp-content/uploads/2008/05/send.PNG' target="_blank"><img src='http://rusanu.com/wp-content/uploads/2008/05/send.PNG' alt="Send Message" width="150" title="Click on the image for a full size view" /></a></div>
<p>Now all these would make a nice tool for administering Service Broker, with some richer functionality that SSMS offers. But the I saved the best features for last.</p>
<h2>
The Wizards</h2>
<p>
What makes the Toad SSB tool <strong>really</strong> useful are its wizards:
</p>
<ul>
<li>Transport Security Configuration Wizard</li>
<li>Service Broker Application Wizard</li>
<li>Server Event Notification Application Wizard</li>
<li>Database Event Notification Application Wizard</li>
</ul>
<p>
The Transport Security Configuration Wizard will configure endpoints and transport security between two SQL Server instances, including the exchange of certificates in case of certificate based security. It allows the user to select the desired authentication mode (certificates and/or Windows) and it will take the user step by step through all the necessary settings involved, if needed, including configuring the database master key password, certificate creation etc. The Transport Security Configuration Wizard has to be run only once between any two SQL Server instances.</p>
<div class="post-image"><a href='http://rusanu.com/wp-content/uploads/2008/05/transportsecurity.PNG' target="_blank"><img src='http://rusanu.com/wp-content/uploads/2008/05/transportsecurity.PNG' alt="Transport Security Wizard" width="250" title="Click on the image for a full size view"/></a></div>
<p>
The Service Broker Application Wizard will take you step by step to setup a complete SSB application between two services. It allows you to create or choose an existing service for initiator and target, create or choose an existing contract and it will configure routing and security between the two services. If it detects that the two services are located on separate SQL Server instances it will launch the Transport Security Configuration Wizard if needed in order to configure the transport security separately. Not only that, but if you create a new queue it will also create a stored procedure for activation, based on a template with a message RECEIVE loop that handles the system Error and EndDialog message types and has a placeholder where you have to put the logic to handle your own contract messages.</p>
<div class="post-image"><a href='http://rusanu.com/wp-content/uploads/2008/05/application.PNG' target="_blank"><img src='http://rusanu.com/wp-content/uploads/2008/05/application.PNG' alt="Service Broker Application Wizard" width="250" title="Click on the image for a full size view"/></a></div>
<p>
The two Event Notification Wizards are for configuring a service queue&nbsp; that can handle an event notification. The Wizard will let you choose the event notification scope and type, create an event notification subscription and can even automatically create the service and queue for you, with names derived from the subscription name.</p>
<div class="post-image"><a href='http://rusanu.com/wp-content/uploads/2008/05/eventnotifications.PNG' target="_blank"><img src='http://rusanu.com/wp-content/uploads/2008/05/eventnotifications.PNG' alt="Event Notifications Application Wizard" width="250" title="Click on the image for a full size view"/></a></div>
<h2>Odds and Ends</h2>
<p>I consider the Toad Service Broker Manager a much better alternative than SSMS for Service Broker. This should come as no surprise, after all the SSMS tools come for free with a SQL Server purchase and Quest has to make better tools if is to stay in business. I reckon though I was pleasantly surprised by the depth of coverage Toad&#8217;s has to offer for SSB. The Wizards are awesome and exactly what you need to get yourself started on SSB.</p>
<p>
But there are some odds and ends, some rough edges in the Service Broker Manager too. Some examples would be that the Service Broker Application Wizard forces you to choose a stored procedure for activation, omitting cases like an external application that might read the queue. Also the Service Broker Application wizard <strong><em>only</em></strong> lets you configure public security between services and cannot configure secure conversations. How ironic that the Service Listing Manager only lets you configure secure conversations and cannot deal with public security :) <i>[Correction 5/22: there is a separate <a href="http://rusanu.com/2008/05/22/dialog-security-configuration-wizard-in-toad">Dialog Security Wizard</a> that allows you to configure both anonymous and full security]</i></p>
<p>
There are also some problems I encountered and had to work my way around. For instance the Transport Security Wizard uses the computer administrative share (MACHINE\Admin$) to exchange certificates, which requires the SQL Server instance to be able to access this share. Most times certificates are deployed exactly because the two machines <em>cannot</em> authenticate using Windows, so access to the administrative share is unlikely.</p>
<p>
I must also add that this post is not an endorsement of any sort nor payed advertising. I simply heard about the tools and downloaded the trial version to test them, just like any of you can, but I am very pleased with what I see and I had to share with my readers.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2008/05/16/viable-tools-for-service-broker/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SQLPASS 2008 Slides and Demo</title>
		<link>http://rusanu.com/2008/04/15/sqlpass-2008-slides-and-demo/</link>
		<comments>http://rusanu.com/2008/04/15/sqlpass-2008-slides-and-demo/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 21:39:46 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://rusanu.com/2008/04/15/sqlpass-2008-slides-and-demo/</guid>
		<description><![CDATA[Copyright Remus Rusanu 2008. Visit the original article at http://rusanu.com/2008/04/15/sqlpass-2008-slides-and-demo/&#160;&#160;I have uploaded here the slides I presented today at SQLPASS on Service Broker Administration, Monitoring and Troubleshooting:
SQLPASS 2008 Slides
]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2008. Visit the original article at <a href="http://rusanu.com/2008/04/15/sqlpass-2008-slides-and-demo/">http://rusanu.com/2008/04/15/sqlpass-2008-slides-and-demo/</a></p>&nbsp;&nbsp;<br><p>I have uploaded here the slides I presented today at SQLPASS on Service Broker Administration, Monitoring and Troubleshooting:<br />
<a href='http://rusanu.com/wp-content/uploads/2008/04/sqlpass2008.zip' title='SQLPASS 2008 Slides'>SQLPASS 2008 Slides</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2008/04/15/sqlpass-2008-slides-and-demo/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Service Broker Administration, Monitoring and Troubleshooting</title>
		<link>http://rusanu.com/2008/04/13/service-broker-administration-monitoring-and-troubleshooting/</link>
		<comments>http://rusanu.com/2008/04/13/service-broker-administration-monitoring-and-troubleshooting/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 19:03:16 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://rusanu.com/2008/04/13/service-broker-administration-monitoring-and-troubleshooting/</guid>
		<description><![CDATA[Copyright Remus Rusanu 2008. Visit the original article at http://rusanu.com/2008/04/13/service-broker-administration-monitoring-and-troubleshooting/&#160;&#160;I&#8217;m about to embark on my flight to Germany for this year PASS conference. My 2008 session is Service Broker Administration, Monitoring and Troubleshooting and is scheduled on Tuesday April 15 at 10:45, see
http://www.european-pass-conference.com/agenda.aspx
If you are at this years European PASS Conference stop by and say [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2008. Visit the original article at <a href="http://rusanu.com/2008/04/13/service-broker-administration-monitoring-and-troubleshooting/">http://rusanu.com/2008/04/13/service-broker-administration-monitoring-and-troubleshooting/</a></p>&nbsp;&nbsp;<br><p>I&#8217;m about to embark on my flight to Germany for this year PASS conference. My 2008 session is <b>Service Broker Administration, Monitoring and Troubleshooting</b> and is scheduled on Tuesday April 15 at 10:45, see<br />
<a href="http://www.european-pass-conference.com/agenda.aspx">http://www.european-pass-conference.com/agenda.aspx</a></p>
<p>If you are at this years European PASS Conference stop by and say Hello!</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2008/04/13/service-broker-administration-monitoring-and-troubleshooting/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
