<?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>Thu, 11 Jun 2009 10:40:19 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Exception handling and nested transactions</title>
		<link>http://rusanu.com/2009/06/11/exception-handling-and-nested-transactions/</link>
		<comments>http://rusanu.com/2009/06/11/exception-handling-and-nested-transactions/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 10:40:19 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Samples]]></category>

		<category><![CDATA[@@trancount]]></category>

		<category><![CDATA[begin catch]]></category>

		<category><![CDATA[begin try]]></category>

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

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

		<category><![CDATA[t-sql]]></category>

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

		<guid isPermaLink="false">http://rusanu.com/?p=430</guid>
		<description><![CDATA[Copyright Remus Rusanu 2009. Visit the original article at http://rusanu.com/2009/06/11/exception-handling-and-nested-transactions/&#160;&#160;I wanted to use a template for writing procedures that behave as intuitively as possible in regard to nested transactions. My goals were:

The procedure template should wrap all the work done in the procedure in a transaction.
The procedures should be able to call each other and [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2009. Visit the original article at <a href="http://rusanu.com/2009/06/11/exception-handling-and-nested-transactions/">http://rusanu.com/2009/06/11/exception-handling-and-nested-transactions/</a></p>&nbsp;&nbsp;<br><p>I wanted to use a template for writing procedures that behave as intuitively as possible in regard to nested transactions. My goals were:</p>
<ul>
<li>The procedure template should wrap all the work done in the procedure in a transaction.</li>
<li>The procedures should be able to call each other and the calee should nest its transaction inside the outer caller transaction.</li>
<li>The procedure should only rollback its own work in case of exception, if possible.</li>
<li>The caller should be able to resume and continue even if the calee rolled back ts work.</li>
</ul>
<p>My solution is to use a either a transactions or a savepoint, depending on the value of @@TRANCOUNT at procedure start. The procedures start a new transaction if no transaction is pending. Otherwise they simply create a savepoint. On exit the procedure commits the transaction they started (if they started one), otherwise they simply exit. On exception, if the transaction is not doomed, the procedure either rolls back (if it started the transaction), or rolls back to the savepoint it created (if calee already provided a transaction).</p>
<p>Because of the use of savepoints this template does not work in all situations, since there are cases like distributed transactions, that cannot be mixed with savepoints. But for the average run of the mill procedures, this template has saved me very well.</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">create</span><span style="color:Black">&nbsp;</span><span style="color:Blue">procedure</span><span style="color:Black">&nbsp;[usp_my_procedure_name]
</span><span style="color:Blue">as
begin
</span><span style="color:Black">	</span><span style="color:Blue">set</span><span style="color:Black">&nbsp;</span><span style="color:Blue">nocount</span><span style="color:Black">&nbsp;</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">&nbsp;@trancount&nbsp;</span><span style="color:Blue">int</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">set</span><span style="color:Black">&nbsp;@trancount&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;</span><span style="color:Fuchsia">@@trancount</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">begin</span><span style="color:Black">&nbsp;</span><span style="color:Blue">try
</span><span style="color:Black">		</span><span style="color:Blue">if</span><span style="color:Black">&nbsp;@trancount&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;0
			</span><span style="color:Blue">begin</span><span style="color:Black">&nbsp;</span><span style="color:Blue">transaction
</span><span style="color:Black">		</span><span style="color:Blue">else
</span><span style="color:Black">			</span><span style="color:Blue">save</span><span style="color:Black">&nbsp;</span><span style="color:Blue">transaction</span><span style="color:Black">&nbsp;usp_my_procedure_name</span><span style="color:Gray">;

</span><span style="color:Black">		</span><span style="color:Green">--&nbsp;Do&nbsp;the&nbsp;actual&nbsp;work&nbsp;here
</span><span style="color:Black">	
lbexit</span><span style="color:Gray">:
</span><span style="color:Black">		</span><span style="color:Blue">if</span><span style="color:Black">&nbsp;@trancount&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;0	
			</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">&nbsp;</span><span style="color:Blue">try
</span><span style="color:Black">	</span><span style="color:Blue">begin</span><span style="color:Black">&nbsp;</span><span style="color:Blue">catch
</span><span style="color:Black">		</span><span style="color:Blue">declare</span><span style="color:Black">&nbsp;@error&nbsp;</span><span style="color:Blue">int</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;@message&nbsp;</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">&nbsp;@xstate&nbsp;</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">&nbsp;@error&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;</span><span style="color:Fuchsia">ERROR_NUMBER</span><span style="color:Gray">(),</span><span style="color:Black">&nbsp;@message&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;</span><span style="color:Fuchsia">ERROR_MESSAGE</span><span style="color:Gray">(),</span><span style="color:Black">&nbsp;@xstate&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;</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">&nbsp;@xstate&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;</span><span style="color:Gray">-</span><span style="color:Black">1
			</span><span style="color:Blue">rollback</span><span style="color:Gray">;
</span><span style="color:Black">		</span><span style="color:Blue">if</span><span style="color:Black">&nbsp;@xstate&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;1&nbsp;</span><span style="color:Gray">and</span><span style="color:Black">&nbsp;@trancount&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;0
			</span><span style="color:Blue">rollback
</span><span style="color:Black">		</span><span style="color:Blue">if</span><span style="color:Black">&nbsp;@xstate&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;1&nbsp;</span><span style="color:Gray">and</span><span style="color:Black">&nbsp;@trancount&nbsp;</span><span style="color:Gray">&gt;</span><span style="color:Black">&nbsp;0
			</span><span style="color:Blue">rollback</span><span style="color:Black">&nbsp;</span><span style="color:Blue">transaction</span><span style="color:Black">&nbsp;usp_my_procedure_name</span><span style="color:Gray">;

</span><span style="color:Black">		</span><span style="color:Blue">raiserror</span><span style="color:Black">&nbsp;</span><span style="color:Gray">(</span><span style="color:Red">'usp_my_procedure_name:&nbsp;%d:&nbsp;%s'</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;11</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;1</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;@error</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;@message</span><span style="color:Gray">)</span><span style="color:Black">&nbsp;</span><span style="color:Gray">;
</span><span style="color:Black">		</span><span style="color:Blue">return</span><span style="color:Gray">;
</span><span style="color:Black">	</span><span style="color:Blue">end</span><span style="color:Black">&nbsp;</span><span style="color:Blue">catch</span><span style="color:Black">	
</span><span style="color:Blue">end</span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2009/06/11/exception-handling-and-nested-transactions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>%%lockres%% collision probability magic marker: 16,777,215</title>
		<link>http://rusanu.com/2009/05/29/lockres-collision-probability-magic-marker-16777215/</link>
		<comments>http://rusanu.com/2009/05/29/lockres-collision-probability-magic-marker-16777215/#comments</comments>
		<pubDate>Fri, 29 May 2009 00:41:08 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Troubleshooting]]></category>

		<category><![CDATA[birthday attack]]></category>

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

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

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

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

		<guid isPermaLink="false">http://rusanu.com/?p=410</guid>
		<description><![CDATA[Copyright Remus Rusanu 2009. Visit the original article at http://rusanu.com/2009/05/29/lockres-collision-probability-magic-marker-16777215/&#160;&#160;@jrowlandjones blogged about a dubious deadlock case. I recommend this article as is correct and presents a somewhat esoteric case of deadlock: the resource hash collision. The lock manager in SQL Server doesn&#8217;t know what it locks, it just locks &#8216;resources&#8217; (basically strings). It is the [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2009. Visit the original article at <a href="http://rusanu.com/2009/05/29/lockres-collision-probability-magic-marker-16777215/">http://rusanu.com/2009/05/29/lockres-collision-probability-magic-marker-16777215/</a></p>&nbsp;&nbsp;<br><p><a href="http://blogs.conchango.com/jamesrowlandjones" target="_blank">@jrowlandjones</a> blogged about a <a href="http://blogs.conchango.com/jamesrowlandjones/archive/2009/05/28/the-curious-case-of-the-dubious-deadlock-and-the-not-so-logical-lock.aspx" target="_blank">dubious deadlock case.</a> I recommend this article as is correct and presents a somewhat esoteric case of deadlock: the resource hash collision. The lock manager in SQL Server doesn&#8217;t know what it locks, it just locks &#8216;resources&#8217; (basically <b>strings</b>). It is the job of higher level components like the the access methods of the storage engine to present the &#8216;resource&#8217; to the lock manager and ask for the desired lock. When locking rows in a heap or a b-tree the storage engine will synthesize a &#8216;resource&#8217; from the record identifier. Since these resources have a limited length, the storage engine has to reduce the effective length of a key to the maximum length is allowed to present to the lock manager, and that means that the record&#8217;s key will be reduced to 6 bytes. This is achieved by hashing the key into a 6 byte hash value. Nothing spectacular here.</p>
<p>But  if you have a table with a key of length 50 bytes and its reduced to 6 bytes, you may hit a collision. So how likely is this to happen?</p>
<p>On 6 bytes there are 281,474,976,710,656 distinct possible values. Its a pretty big number? Not that big actually. If we meet at a party and I say &#8216;I bet somebody in the room shares your birthday&#8217; would you bet against me? You probably should :) What if I change my question to &#8216;I bet there are two people in this room that share the birthday!&#8217;? Now I will probably take your money. This is called a &#8216;<a href="http://en.wikipedia.org/wiki/Birthday_attack" target="_blank">meet-in-the-middle</a>&#8216; attack in cryptography and basically it says that you get a 50% collision probability at half the hash length. So the SQL %%lockres%% hash will produce two records with same hash, with a 50% probability, out of the table, any table, of only 16,777,215 record. That suddenly doesn&#8217;t look like a cosmic constant, does it? And keep in mind that this is the absolutely maximum value, where the key has a perfectly random distribution. In reality keys are not that random after all. Take Jame&#8217;s example: a datetime (8 bytes), a country code (1 byte), a group code (2 bytes) and a random id (4 bytes). From these 15 bytes quite a few are actually constant: eg. every date between 2000 and 2010 has the first 4 bytes identical (0&#215;00) and the 5th byte only has two possible values (0&#215;08 or 0&#215;09). If from the other codes (country, group) we use only 50% of the possible values, then in effect we use, generously, just 10 bytes of the 15 bytes of the key. This means the table has a 50% collision probability at only about 11 million records. considering that he was doing a &#8216;paltry&#8217; 900 million records upload, no wonder he got collisions&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2009/05/29/lockres-collision-probability-magic-marker-16777215/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Have you met ï»¿ ? Say hello to my BOM</title>
		<link>http://rusanu.com/2009/05/21/have-you-met-i%c2%bb%c2%bf-say-hello-to-my-bom/</link>
		<comments>http://rusanu.com/2009/05/21/have-you-met-i%c2%bb%c2%bf-say-hello-to-my-bom/#comments</comments>
		<pubDate>Thu, 21 May 2009 23:07:08 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[byte order mark]]></category>

		<category><![CDATA[ï»¿]]></category>

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

		<guid isPermaLink="false">http://rusanu.com/?p=398</guid>
		<description><![CDATA[Copyright Remus Rusanu 2009. Visit the original article at http://rusanu.com/2009/05/21/have-you-met-i%c2%bb%c2%bf-say-hello-to-my-bom/&#160;&#160;I recently had to look at a problem where a programmer writing a tool to parse some XML produced by a service written by me was complaining that my service serves invalid XML. All my documents seemed mangled and started with &#239;&#187;&#191;. I couldn&#8217;t help but [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2009. Visit the original article at <a href="http://rusanu.com/2009/05/21/have-you-met-i%c2%bb%c2%bf-say-hello-to-my-bom/">http://rusanu.com/2009/05/21/have-you-met-i%c2%bb%c2%bf-say-hello-to-my-bom/</a></p>&nbsp;&nbsp;<br><p>I recently had to look at a problem where a programmer writing a tool to parse some XML produced by a service written by me was complaining that my service serves invalid XML. All my documents seemed mangled and started with &#239;&#187;&#191;. I couldn&#8217;t help but smile. So what is this mysterious sequence &#239;&#187;&#191;? Well, lets check the <a href="http://en.wikipedia.org/wiki/ISO_8859-1" target="_blank">ISO-8859-1</a> character encoding, commonly known as &#8216;Latin alphabet&#8217; and often confused with the Windows code page 1252: &#239; is the character corresponding to 0xEF (decimal 239), &#187; is 0xBB (decimal 187) and &#191; is 0xBF (decimal 191). So the mysterious sequence is 0xEFBBBF. Does it look familiar now? It should, this is the <a href="http://en.wikipedia.org/wiki/Byte-order_mark" target="_blank">UTF-8 Byte Order Mark</a>. Moral: if you consume and parse XML, make sure you consume it as XML, not as text. All XML libraries I know of correctly understand and parse the BOM. The only problems I&#8217;ve seen are from hand written &#8216;parsers&#8217; that treat XML as a string (and most often fail to accommodate namespaces too&#8230;).</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2009/05/21/have-you-met-i%c2%bb%c2%bf-say-hello-to-my-bom/feed/</wfw:commentRss>
		</item>
		<item>
		<title>stackoverflow.com: how to execute well on a good idea</title>
		<link>http://rusanu.com/2009/05/18/stackoverflowcom-how-to-execute-well-on-a-good-idea/</link>
		<comments>http://rusanu.com/2009/05/18/stackoverflowcom-how-to-execute-well-on-a-good-idea/#comments</comments>
		<pubDate>Mon, 18 May 2009 16:25:26 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

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

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

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

		<category><![CDATA[stackoverflow.com]]></category>

		<guid isPermaLink="false">http://rusanu.com/?p=393</guid>
		<description><![CDATA[Copyright Remus Rusanu 2009. Visit the original article at http://rusanu.com/2009/05/18/stackoverflowcom-how-to-execute-well-on-a-good-idea/&#160;&#160;Some time ago I started noticing on my google searches a newcomer: stackoverflow.com. At first I dismissed this as yet another SEO hack to divert traffic to some re-syndicated content of the old user groups and forums. But I was wrong. Turns out stackoverflow.com is an [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2009. Visit the original article at <a href="http://rusanu.com/2009/05/18/stackoverflowcom-how-to-execute-well-on-a-good-idea/">http://rusanu.com/2009/05/18/stackoverflowcom-how-to-execute-well-on-a-good-idea/</a></p>&nbsp;&nbsp;<br><p>Some time ago I started noticing on my google searches a newcomer: <a href="stackoverflow.com" target="_blank">stackoverflow.com</a>. At first I dismissed this as yet another SEO hack to divert traffic to some re-syndicated content of the old user groups and forums. But I was wrong. Turns out stackoverflow.com is an enterprise backed by well known industry names like Joel Spolsky of <a href="http://www.joelonsoftware.com/" target="_blank">Joel on Software</a> fame. Apparently I&#8217;ve been living in a cave, since this new site is quite popular and even doing a <a href="http://stackoverflow.carsonified.com/tickets.html" target="_blank">Stack Overflow DevDays tour</a>!</p>
<p>Now being that I&#8217;m a forums and newsgroup addict with a long history of MSDN abuse, I had to join this one and start showing off my amazing knowledge and wit. OK, you can all stop laughing now. I am a noob, so what? I still love to answer questions ;) and not actually knowing the answer has never a stop for me. Imagination is more important than knowledge!</p>
<p>I spend now about 4 days on stackoverflow.com and I must say that I&#8217;m impressed. First of all, they do offer innovation in how the content is gathered and presented. The hierarchical forums model was obviously obsolete and it was showing its age: new users have a hard time figuring out which forum is the right one, monitoring the questions is difficult as the volume increases, there are often questions that obviously span multiple topics and picking the one forum for it severely restricts the exposure of the said question, and implicitly the quality of responses. Instead stackoverflow.com goes for a tags based model. When you ask a question you choose tags relevant for it and you can mix and match tags as diverse as linq, objective-c and php in one single question.</p>
<p>Now tags based contents isn&#8217;t exactly new, but the way is executed on stackoverflow.com takes it to a new level. Of course they offer tags based browsing of topics. But they also keep track of the tags you must often interact with (ask or answer). You can browse tags within current tags to get the questions that cover multiple tags of your interest. And the tags system is completely open, anyone can create new tags and they even have awards for successful tags.</p>
<p>The next innovation idea I like is how they try to blend the line between wiki and forums. Topics that prove to be popular and have a good answer can be promoted to wiki entries. This makes the entry serve the same reference role the &#8217;sticky&#8217; posts serve in forums, but with better functionality. Also rooted in wikis (and craigslist too) is the idea of member provided social policing for the content: answers get voted up or down by community members. Not only that, but questions also get to be voted up or down, which is something I have not seen elsewhere. And ultimately questions can be closed, responses deleted. How is this different from the forum administrators? These people are not administrators, are just ordinary community members. You gain reputation, you earn privileges.</p>
<p>The reputation system is not new, by now almost any community forum has a reputation points system in place. But with stackoverflow.com they added also a system of badges that I feel comes straight from the video games world of achievements and vanity awards : you get bronze, silver or gold badges for achieving tasks in the stackoverflow.com ecosystem. You get your Teacher badge for answering a question and receiving an Up vote, you get the Student badge for answering a question that receives an up vote, or even a gold badge of Great Answer if it gets voted up 100 times. Now these are, of course, vanity awards. We all know though how efficient they are in keeping users hooked in! Me, I&#8217;m eager to get my Critic badge&#8230;</p>
<p>The only serious thing missing is the RSS syndication of views, but I hear is in the plans.</p>
<p>But most impressive is the quality of execution on these good ideas. The site is fast and responsive. It provides suggestion to similar questions as you type yours. It provides fast navigation to your questions and answers. Visual notifications for changes since your previous check. Suggestions for related topics (ie. common tags). Is true that I don&#8217;t know how many users it carries. Judging from the ~30K Teacher badges, I&#8217;d guess some 50k users registered and active, as a conservative estimate.</p>
<p>Is also nice to see such an effort started from a grassroots movement, and not from the political sponsorship of an industry player. Today&#8217;s developer has to deal in the course of a single day with an NSConnection question, related to an issue of Appache .htaccess mod_rewrite and PHP cookie handling and resulting in a SQL Server access problem. A site like the Social on MSDN would not happily sponsor and encourage such questions, nor would it nurture and grow the community leaders that can answer such end-to-end and cross platform questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2009/05/18/stackoverflowcom-how-to-execute-well-on-a-good-idea/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Read/Write deadlock</title>
		<link>http://rusanu.com/2009/05/16/readwrite-deadlock/</link>
		<comments>http://rusanu.com/2009/05/16/readwrite-deadlock/#comments</comments>
		<pubDate>Sat, 16 May 2009 03:13:03 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Troubleshooting]]></category>

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

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

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

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

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

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

		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://rusanu.com/?p=361</guid>
		<description><![CDATA[Copyright Remus Rusanu 2009. Visit the original article at http://rusanu.com/2009/05/16/readwrite-deadlock/&#160;&#160;How does a simple SELECT deadlock with an UPDATE? Surprisingly, they can deadlock even on well tuned systems that does not do spurious table scans. The answer is very simple: when the read and the write use two distinct access paths to reach the same key [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2009. Visit the original article at <a href="http://rusanu.com/2009/05/16/readwrite-deadlock/">http://rusanu.com/2009/05/16/readwrite-deadlock/</a></p>&nbsp;&nbsp;<br><p>How does a simple SELECT deadlock with an UPDATE? Surprisingly, they can deadlock even on well tuned systems that does not do spurious table scans. The answer is very simple: when the read and the write use two distinct access paths to reach the same key and they use them in reverse order. Lets consider a simple example: we have a table with a clustered index and an non-clustered index. The reader (T1) seeks a key in the non-clustered index and then needs to look up the clustered index to retrieve an additional column required by the SELECT projection list. The writer (T2) is updating the clustered index and then needs to perform an index maintenance operation on the non-clustered index. So T1 holds an S lock for the key K on the non-clustered index and wants an S lock on the same key K on the clustered index. T2 has an X lock on the key K on the clustered index and wants an X lock on same key K on the non-clustered index. Deadlock, T1 will be chosen as a victim. So you see, there are no complex queries involved, no suboptimal scan operations, no lock escalation nor page locks involved. Simple, correctly written queries may deadlock if doing read/write operations on the same key on a table with two indexes. Lets show this in an example:</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">use</span><span style="color:Black">&nbsp;tempdb</span><span style="color:Gray">;
</span><span style="color:Black">go

</span><span style="color:Blue">create</span><span style="color:Black">&nbsp;</span><span style="color:Blue">table</span><span style="color:Black">&nbsp;TestDeadlock&nbsp;</span><span style="color:Gray">(</span><span style="color:Black">TestId&nbsp;</span><span style="color:Blue">int</span><span style="color:Black">&nbsp;</span><span style="color:Blue">identity</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">constraint</span><span style="color:Black">&nbsp;cdxTestDealockTestId&nbsp;</span><span style="color:Blue">primary</span><span style="color:Black">&nbsp;</span><span style="color:Blue">key</span><span style="color:Black">&nbsp;</span><span style="color:Blue">clustered</span><span style="color:Black">&nbsp;</span><span style="color:Gray">(</span><span style="color:Black">TestId</span><span style="color:Gray">),
</span><span style="color:Black">	ColA&nbsp;</span><span style="color:Blue">varchar</span><span style="color:Gray">(</span><span style="color:Black">10</span><span style="color:Gray">)</span><span style="color:Black">&nbsp;</span><span style="color:Gray">not</span><span style="color:Black">&nbsp;</span><span style="color:Gray">null,
</span><span style="color:Black">	ColB&nbsp;</span><span style="color:Blue">varchar</span><span style="color:Gray">(</span><span style="color:Black">10</span><span style="color:Gray">)</span><span style="color:Black">&nbsp;</span><span style="color:Gray">not</span><span style="color:Black">&nbsp;</span><span style="color:Gray">null);
</span><span style="color:Black">go

</span><span style="color:Blue">create</span><span style="color:Black">&nbsp;</span><span style="color:Blue">index</span><span style="color:Black">&nbsp;idxTestDeadlockColA&nbsp;</span><span style="color:Blue">on</span><span style="color:Black">&nbsp;TestDeadlock</span><span style="color:Gray">(</span><span style="color:Black">ColA</span><span style="color:Gray">);
</span><span style="color:Black">go</span>
</pre>
<p>Now lets populate the table with some random data:</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">set</span><span style="color:Black">&nbsp;</span><span style="color:Blue">nocount</span><span style="color:Black">&nbsp;</span><span style="color:Blue">on</span><span style="color:Gray">;
</span><span style="color:Blue">insert</span><span style="color:Black">&nbsp;</span><span style="color:Blue">into</span><span style="color:Black">&nbsp;TestDeadlock&nbsp;</span><span style="color:Gray">(</span><span style="color:Black">ColA</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;ColB</span><span style="color:Gray">)</span><span style="color:Black">&nbsp;</span><span style="color:Blue">values</span><span style="color:Black">&nbsp;</span><span style="color:Gray">(</span><span style="color:Red">'A'</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;</span><span style="color:Red">'B'</span><span style="color:Gray">);

</span><span style="color:Blue">declare</span><span style="color:Black">&nbsp;@i&nbsp;</span><span style="color:Blue">int</span><span style="color:Gray">;
</span><span style="color:Blue">select</span><span style="color:Black">&nbsp;@i&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;0</span><span style="color:Gray">;
</span><span style="color:Blue">while</span><span style="color:Black">&nbsp;@i&nbsp;</span><span style="color:Gray">&lt;</span><span style="color:Black">&nbsp;10000
</span><span style="color:Blue">begin
</span><span style="color:Black">	</span><span style="color:Blue">insert</span><span style="color:Black">&nbsp;</span><span style="color:Blue">into</span><span style="color:Black">&nbsp;TestDeadlock&nbsp;</span><span style="color:Gray">(</span><span style="color:Black">ColA</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;ColB</span><span style="color:Gray">)</span><span style="color:Black">&nbsp;</span><span style="color:Blue">values</span><span style="color:Black">&nbsp;</span><span style="color:Gray">(</span><span style="color:Fuchsia">rand</span><span style="color:Gray">()*</span><span style="color:Black">99999</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;</span><span style="color:Fuchsia">rand</span><span style="color:Gray">()*</span><span style="color:Black">99999</span><span style="color:Gray">);
</span><span style="color:Black">	</span><span style="color:Blue">select</span><span style="color:Black">&nbsp;@i&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;@i</span><span style="color:Gray">+</span><span style="color:Black">1</span><span style="color:Gray">;
</span><span style="color:Blue">end
</span><span style="color:Black">go</span>
</pre>
<p>Now open two query windows, and on the first one start this loop (the Reader):</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">set</span><span style="color:Black">&nbsp;</span><span style="color:Blue">nocount</span><span style="color:Black">&nbsp;</span><span style="color:Blue">on</span><span style="color:Gray">;
</span><span style="color:Blue">while</span><span style="color:Black">&nbsp;</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:Blue">begin
</span><span style="color:Black">	</span><span style="color:Blue">declare</span><span style="color:Black">&nbsp;@B&nbsp;</span><span style="color:Blue">varchar</span><span style="color:Gray">(</span><span style="color:Black">10</span><span style="color:Gray">);
</span><span style="color:Black">	</span><span style="color:Blue">select</span><span style="color:Black">&nbsp;@B&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;ColB&nbsp;</span><span style="color:Blue">from</span><span style="color:Black">&nbsp;TestDeadlock&nbsp;</span><span style="color:Blue">where</span><span style="color:Black">&nbsp;ColA</span><span style="color:Gray">=</span><span style="color:Red">'A'</span><span style="color:Gray">;
</span><span style="color:Blue">end</span>
</pre>
<p>On the second one, start this loop (the Writer):</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">set</span><span style="color:Black">&nbsp;</span><span style="color:Blue">nocount</span><span style="color:Black">&nbsp;</span><span style="color:Blue">on</span><span style="color:Gray">;
</span><span style="color:Blue">while</span><span style="color:Black">&nbsp;</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:Blue">begin
</span><span style="color:Black">	</span><span style="color:Blue">update</span><span style="color:Black">&nbsp;TestDeadlock&nbsp;</span><span style="color:Blue">set</span><span style="color:Black">&nbsp;ColA</span><span style="color:Gray">=</span><span style="color:Red">'A'</span><span style="color:Black">&nbsp;</span><span style="color:Blue">where</span><span style="color:Black">&nbsp;TestId</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">update</span><span style="color:Black">&nbsp;TestDeadlock&nbsp;</span><span style="color:Blue">set</span><span style="color:Black">&nbsp;ColA</span><span style="color:Gray">=</span><span style="color:Red">'B'</span><span style="color:Black">&nbsp;</span><span style="color:Blue">where</span><span style="color:Black">&nbsp;TestId</span><span style="color:Gray">=</span><span style="color:Black">1</span><span style="color:Gray">;
</span><span style="color:Blue">end</span>
</pre>
<p>Now switch back to the first query window and there you have it:</p>
<pre>
<span style="color:Red">Msg 1205, Level 13, State 51, Line 6
Transaction (Process ID 52) was deadlocked on lock resources with another process
and has been chosen as the deadlock victim. Rerun the transaction.</span>
</pre>
<p>Looking at the deadlock graph will show exactly the situation I described:</p>
<p><a href="http://rusanu.com/wp-content/uploads/2009/05/testdeadlock.png"><img src="http://rusanu.com/wp-content/uploads/2009/05/testdeadlock-300x61.png" alt="" title="testdeadlock" width="300" height="61" class="alignnone size-medium wp-image-362" /></a></p>
<p>SPID 52 has an KEY S lock on idxTestDeadlockColA and wants an KEY S lock on cdxTestDeadlockTestId, SPID 53 has the KEY X lock on cdxTestDeadlockTestId and wants a KEY X lock on dxTestDeadlockColA. SPID 52 is chosen as victim since it performed less writes than SPID 53.</p>
<p>Why is this kind of deadlock not more prevalent? The key ingredient is that both T1 and T2 have to go after the same &#8216;key&#8217;. On a real life situation, this is just a matter of probabilities. If the write application pattern is to write new keys and to look up old ones, it is unlikely to happen. If the number of keys is very large then the probability of overlap is low (of course it increases around &#8216;hot spots&#8217;,  for example a hot post on your website). In my recent experience on of the main culprits for these deadlocks are the hit counters prevalent in many content management systems (increment number of hits a page or image was shown).</p>
<p>Unfortunately things are not always so easy to understand and to explain. The same problem can manifest itself in the disguise of a join. The key learning to take home is this: neither the reads nor the writes do not acquire locks in an atomic fashion on all indexes of a table (that would be impossible, of course!). Whenever two transactions acquire these locks in a different order from one another, they open themselves to a deadlock.</p>
<p>The proper fix varies, of course. Things to consider are:</p>
<ul>
<li>eliminate unnecessary columns from reader&#8217;s projection so he does not have to look up the clustered index</li>
<li>add required columns as contained columns to the non-clustered index to make the index covering, again so that the reader does not have look up the clustered index</li>
<li>avoid updates that have to maintain the non-clustered index</li>
<p>One final note: why did I add 10000 dummy record in my example? Because otherwise the optimizer would see that the clustered index has only one page and would choose a plan that scans this index instead of seeking the non-clustered index.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2009/05/16/readwrite-deadlock/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Version Control and your Database</title>
		<link>http://rusanu.com/2009/05/15/version-control-and-your-database/</link>
		<comments>http://rusanu.com/2009/05/15/version-control-and-your-database/#comments</comments>
		<pubDate>Fri, 15 May 2009 19:15:41 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Announcements]]></category>

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

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

		<category><![CDATA[source control]]></category>

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

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

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

		<guid isPermaLink="false">http://rusanu.com/?p=358</guid>
		<description><![CDATA[Copyright Remus Rusanu 2009. Visit the original article at http://rusanu.com/2009/05/15/version-control-and-your-database/&#160;&#160;I am still amazed when I walk into a development shop and I ask for their application database script and they offer to extract one for me. Really, your only definition of the database is the database itself? Now you wouldn&#8217;t keep your libraries as object [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2009. Visit the original article at <a href="http://rusanu.com/2009/05/15/version-control-and-your-database/">http://rusanu.com/2009/05/15/version-control-and-your-database/</a></p>&nbsp;&nbsp;<br><p>I am still amazed when I walk into a development shop and I ask for their application database script and they offer to <strong>extract</strong> one for me. Really, your only definition of the database is the database itself? Now you wouldn&#8217;t keep your libraries as object code only and reverse engineer them every time you want to make a change, would you?</p>
<p>Now, all sarcasm aside, why is so hard to keep a database definition as source and keep it under version control? The reason is not that people are dumb, these are bright developers and they would do the right thing if it would fit into their natural work flow. The problem is that the tool set at their disposal as developers (usually the Visual Studio suite) is far far behind the capabilities of the database administration tool set (the SSMS). But the later is focused for the needs of administrators and the natural flow of actions is to visually modify some schema properties (add tables, define indexes etc) in a dialog and then click the &#8216;Do it!&#8217; button. This hides actual scripts going on behind the scenes and does not lend itself naturally to the normal code/build/run/test/commit cycle of the developer desk.</p>
<p>In my development persona I have become acutely aware of the pitfalls of not having sources for my database objects and catalog reference data and not having the benefits of source control versioning. My answer to this problem was to rely on an extended database property (usually named after the application, eg. &#8220;MyApplication DB Version&#8221;) to keep track of the current deployed application schema. Every database schema modification is a version change that is achieved by running a specific script, including the initial deployment from v. NULL to v. 1.0 . First thing I check the current version:</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">SELECT</span><span style="color:Black">&nbsp;[value]&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:Blue">from</span><span style="color:Black">&nbsp;</span><span style="color:Gray">::</span><span style="color:Fuchsia">fn_listextendedproperty</span><span style="color:Black">&nbsp;</span><span style="color:Gray">(
</span><span style="color:Black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:Red">'MyApplication&nbsp;DB&nbsp;Version'</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:Blue">default</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;</span><span style="color:Blue">default</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;</span><span style="color:Blue">default</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;</span><span style="color:Blue">default</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;</span><span style="color:Blue">default</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;</span><span style="color:Blue">default</span><span style="color:Gray">);</span>
</pre>
<p>The application contains a static array of versions and scripts. After it retrieves the current version, it iterates the array until it finds the retrieved version and then runs the script. This script will upgrade the database to the next version, and all the scripts end by setting the new version:</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">EXEC</span><span style="color:Black">&nbsp;</span><span style="color:Maroon">sp_updateextendedproperty</span><span style="color:Black">&nbsp;
	@name&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;N</span><span style="color:Red">'MyApplication&nbsp;DB&nbsp;Version'</span><span style="color:Gray">,</span><span style="color:Black">&nbsp;@value&nbsp;</span><span style="color:Gray">=</span><span style="color:Black">&nbsp;</span><span style="color:Red">'1.2'</span><span style="color:Gray">;
</span><span style="color:Black">GO</span>
</pre>
<p>The application then iterates again until the final version is reached.</p>
<p>The main advantage of this approach is that my deployment script is easy to create and test, and is not peppered with those nasty &#8216;IF EXISTS(&#8230;)&#8217; conditional statements. This scripts become immutable once a version is rolled out. If schema changes then the version is rolled forward and a new script is added with the necessary steps to alter the database to the new schema. The new script is easy to test and hammer into correctness. Start from the current version backup, run the script, fix any problems, then roll back again to the current version backup, run it again until all problems are fixed.</p>
<p>Note that there is no script to deploy directly the current version. A new deployment will deploy by running all the scripts in a row, from v. 0 to v. 1, then v. 1.1, v. 1.2 and so on and so forth. This means that objects in the schema will go trough all their lifetime variations: tables will be created with missing columns then later the column will be added at the v. 1.1 script, stored procedures will go through several incarnations as they are ALTERed into the current final form, reference data will be added then updated or deleted as the application has evolved. This makes for a strange experience for someone monitoring the application deployment, but it certainly pays off from a source maintainability point of view. Also your application can be deployed safely on top of any previous version, as probably your clients will not be all at the last version.</p>
<p>The one major disadvantage of my approach is the lack of a current reference definition of the schema in source. The schema is the result of all the transformations from v. 0 to current version and one cannot simply go into the schema script and see the current definition of any object.</p>
<h2>Visual Studio Database Edition</h2>
<p>Last Wednesday I was at the Pacific Northwest PASS meeting on the MS campus and Barclay Hill presented the newest additions to the VS Database Edition line. I was really impressed. The new functionality supports definition of database schema stored in Transact-SQL scripts (your familiar CREATE statements) and thus fully integrates into source version control, be it via VSTS, <a href="http://ankhsvn.open.collab.net/" target="_blank">AnkhSVN</a> or any SCM of your choice. VS can nou produce a new type of file during the build of your project, a .dbschema file. This is a deployment file containing the definition of your project schema. Coupled with the VSDBCMD command line tool one can take this .dbschema to the customer site, run it and the tool will analyze the current deployed schema, compare it with the .dbschema definition, produce a delta and apply this delta transformation to the database. The tool supports every object in SQL 2008, both at database and instance level. It does not though support replication nor SQL Agent objects, and I think that is fine since they are really tied to deployment site specifics, not to development time.</p>
<p>The fact that the project schema definitions are pure T-SQL and the source control integration means you can setup MSBUILD to produce continuous integration builds and drop a build with the current trunk or branch, including the current .dbschema file. You can then take this .dbschema from the drop location and have it tested at your customer site. Or you can have the build process deploy the project, implicitly running the .dbschema and apply it to your development test database. You can even go fancy and automate the deployment into the staging servers for integration testing. And since MSBUILD supports adding BVTs (build validation tests) to your build drop process, you can also automate that part. I would shy though from going the whole nine yards and have the build process apply the new build on production server, for obvious reasons :). But perhaps this is not so outrageous, specially for shops that develop around one and only one deployment, as is usually the case with web sites. Careful branch management can stage the deployment from development to test, integration and finally production.</p>
<p>Also a good news coming is that the next Visual Studio Database Edition is actually going to be integrated in the Developer Edition. This is really great, since a lot of shops really need this functionality but would not  be willing to pay extra bucks for a separate Database Edition.</p>
<p>If you like to play with this awesome new features I recommend you download the latest Visual Studio Team Systems 2008 Database Edition GDR. Gert has a couple of useful links to the download location, manual, some setup instructions: <a href="http://blogs.msdn.com/gertd/archive/2008/10/27/the-gdr-rc-is-here.aspx" target="_blank">http://blogs.msdn.com/gertd/archive/2008/10/27/the-gdr-rc-is-here.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2009/05/15/version-control-and-your-database/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Birds of a Feather Tweet Together</title>
		<link>http://rusanu.com/2009/05/10/birds-of-a-feather-tweet-together/</link>
		<comments>http://rusanu.com/2009/05/10/birds-of-a-feather-tweet-together/#comments</comments>
		<pubDate>Sun, 10 May 2009 07:31:46 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

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

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

		<guid isPermaLink="false">http://rusanu.com/?p=356</guid>
		<description><![CDATA[Copyright Remus Rusanu 2009. Visit the original article at http://rusanu.com/2009/05/10/birds-of-a-feather-tweet-together/&#160;&#160;@rusanu: I am on Twitter now. Yes, I am another victim of the totally connected social dragon. Since we&#8217;re on the subject here is my LinkedIn profile: http://www.linkedin.com/in/remusrusanu.
]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2009. Visit the original article at <a href="http://rusanu.com/2009/05/10/birds-of-a-feather-tweet-together/">http://rusanu.com/2009/05/10/birds-of-a-feather-tweet-together/</a></p>&nbsp;&nbsp;<br><p><a href="http://twitter.com/rusanu" target="_blank"><strong>@rusanu</strong></a>: I am on Twitter now. Yes, I am another victim of the totally connected social dragon. Since we&#8217;re on the subject here is my LinkedIn profile: <a href="http://www.linkedin.com/in/remusrusanu" target="_blank">http://www.linkedin.com/in/remusrusanu</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2009/05/10/birds-of-a-feather-tweet-together/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A fix for error Cannot find the remote service SqlQueryNotificationService-GUID</title>
		<link>http://rusanu.com/2009/04/18/a-fix-for-error-cannot-find-the-remote-service-sqlquerynotificationservice-guid/</link>
		<comments>http://rusanu.com/2009/04/18/a-fix-for-error-cannot-find-the-remote-service-sqlquerynotificationservice-guid/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 00:53:03 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Announcements]]></category>

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

		<guid isPermaLink="false">http://rusanu.com/?p=345</guid>
		<description><![CDATA[Copyright Remus Rusanu 2009. Visit the original article at http://rusanu.com/2009/04/18/a-fix-for-error-cannot-find-the-remote-service-sqlquerynotificationservice-guid/&#160;&#160;Sometimes your ERRORLOG is peppered with messages complaining about the service SqlQueryNotificationService-&#60;guid&#62; not existing or query notification dialogs being closed because they received an error message with the text Remote service has been dropped. I have blogged about this problem before: http://rusanu.com/2007/11/10/when-it-rains-it-pours/. Unfortunately this problem was [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2009. Visit the original article at <a href="http://rusanu.com/2009/04/18/a-fix-for-error-cannot-find-the-remote-service-sqlquerynotificationservice-guid/">http://rusanu.com/2009/04/18/a-fix-for-error-cannot-find-the-remote-service-sqlquerynotificationservice-guid/</a></p>&nbsp;&nbsp;<br><p>Sometimes your ERRORLOG is peppered with messages complaining about the service SqlQueryNotificationService-&lt;guid&gt; not existing or query notification dialogs being closed because they received an error message with the text <tt>Remote service has been dropped</tt>. I have blogged about this problem before: <a href="http://rusanu.com/2007/11/10/when-it-rains-it-pours/" target="_blank">http://rusanu.com/2007/11/10/when-it-rains-it-pours/</a>. Unfortunately this problem was not under your control as an administrator nor as a developer. It is caused by the way the SqlDependency component of ADO.Net deploys the temporary service, queue and procedure needed for its functioning. The problem could be caused by your application calling SqlDependency.Stop inadvertently but also by simple timing problems: <a href="http://rusanu.com/2008/01/04/sqldependencyonchange-callback-timing/" target="_blank">http://rusanu.com/2008/01/04/sqldependencyonchange-callback-timing/</a>.</p>
<p>Good news: Microsoft has shipped a fix for this issue: <a href="http://support.microsoft.com/kb/958006" target="_blank">http://support.microsoft.com/kb/958006</a>. According to the knowledge base article you need to install the following Cumulative Update depending on your current version of SQL Server deployed:</p>
<ul>
<li>For SQL Server 2005 SP2 you need <a href="http://support.microsoft.com/kb/956854/LN/" target="_blank">CU 10</a>.</li>
<li>For SQL Server 2005 SP3 you need <a href="http://support.microsoft.com/kb/959195/LN/" target="_blank">CU 1</a>.</li>
<li>For SQL Server 2008 you need <a href="http://support.microsoft.com/kb/958186/" target="_blank">CU 2</a>.</li>
</ul>
<p>If you have SQL Server 2008 SP1 deployed you do not need to install any fix because the issue is fixed in SP1 for 2008.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2009/04/18/a-fix-for-error-cannot-find-the-remote-service-sqlquerynotificationservice-guid/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using XSLT to generate Performance Counters code</title>
		<link>http://rusanu.com/2009/04/11/using-xslt-to-generate-performance-counters-code/</link>
		<comments>http://rusanu.com/2009/04/11/using-xslt-to-generate-performance-counters-code/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 00:13:18 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Samples]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[code generation]]></category>

		<category><![CDATA[visual studio]]></category>

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

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

		<guid isPermaLink="false">http://rusanu.com/?p=308</guid>
		<description><![CDATA[Copyright Remus Rusanu 2009. Visit the original article at http://rusanu.com/2009/04/11/using-xslt-to-generate-performance-counters-code/&#160;&#160;Whenever I&#8217;m faced with a project in which I have to create a lot of tedious and repeating code I turn to the power of XML and XSLT. Rather than copy/paste the same code over and over again, just to end up with a refactoring and [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2009. Visit the original article at <a href="http://rusanu.com/2009/04/11/using-xslt-to-generate-performance-counters-code/">http://rusanu.com/2009/04/11/using-xslt-to-generate-performance-counters-code/</a></p>&nbsp;&nbsp;<br><p>Whenever I&#8217;m faced with a project in which I have to create a lot of tedious and repeating code I turn to the power of XML and XSLT. Rather than copy/paste the same code over and over again, just to end up with a refactoring and maintenance nightmare, I create an XML definition file and an XSLT transformation. I am then free to add new elements to the XML definition or to change the way the final code is generated from the XSLT transformation. This can be fully integrated with Visual Studio so that the code generation happens at project build time and the environment shows the generated code as a dependency of the XML definition file.</p>
<p>A few examples of how I&#8217;m using this code generation via XSLT are:</p>
<dl>
<dt>Data Access Layer</dt>
<dd>I know this will raise quite a few eyebrows, but I always write my own data access layer from scratch and is generated via XSLT.</dd>
<dt>Performance Counters</dt>
<dd>I create all my performance counters objects via XSLT generation, automating the process of defining/installing them and the access to emit and consume the counter values.</dd>
<dt>Wire Frames</dt>
<dd>In any project that has networking communication I access the wire format from classes generated via XSLT that take care of serialization and validation.</dd>
</dl>
<p>For example I&#8217;ll show how to create a class library that can be added to your project to expose Performance Counters from your application.</p>
<h3>MSXSL.EXE</h3>
<p>To start you&#8217;ll need to download the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=2FB55371-C94E-4373-B0E9-DB4816552E41&#038;displaylang=en" target="_blank">Command Line Transformation Utility (msxsl.exe)</a>. This is a command line tool that takes an XML and an XSLT file as input and produces the result transformation. As a side note I actually use the same technique on non-Windows platforms, but there of course I use the <a href="http://www.xmlsoft.org/" target="_blank">xsltproc</a> utility.</p>
<p>msxsl.exe has to be placed in a convenient location from where it is accessible by Visual Studio. Download the file and place it in Visual Studio&#8217;s Common\IDE folder because the build environment has a macro for it: <tt>$DevEnvDir</tt>.</p>
<h3>The Project</h3>
<p>Create a new project in Visual Studio. Choose a C# Class Library type project and save it as &#8220;PerformanceCounters&#8221;. After the project is created, add to it two new files, from menu <tt>Project/Add New  Item</tt> or press <tt>Ctrl+Shift+A</tt>. For the first file choose an <tt>XML file</tt> type and name it <tt>Counters.xml</tt>. For the second one choose an <tt>XSLT file</tt> type and name it <tt>Counters.xslt</aa>.</p>
<p>Next add the step that actually performs XSLT transformation to the project build process. Go to the project properties, either from menu <tt>Project/PerformcanceCounters properties&#8230;</tt> or right-click the project in the Solutions Explorer and select <tt>Properties</tt> from the context menu. Choose the <tt>Build Events</tt> pane in the right side navigation tab to get to the pre-build and post-build project events. Click the <tt>Edit pre-build&#8230;</tt> button and add the following command:</p>
<pre>"$(DevEnvDir)msxsl.exe" "$(ProjectDir)Counters.xml" "$(ProjectDir)Counters.xslt"
-o "$(ProjectDir)CountersGenerated.cs"
</pre>
<p>This command will run the command line transformation tool (msxsl.exe) and generate the <tt>CountersGenerated.cs</tt> file each time the project is built, prior to the project being actually built.</p>
<p><a href="http://rusanu.com/wp-content/uploads/2009/04/prebuildevent.png"><img src="http://rusanu.com/wp-content/uploads/2009/04/prebuildevent.png" alt="" title="prebuildevent" width="150" class="alignnone size-thumbnail wp-image-314" /></a></p>
<p>Save and close the project properties. The last thing we need now is to add the generated <tt>CountersGenerated.cs</tt> file to our project. However, we do not want this to be treated as an ordinary source file, we want the IDE to recognize that is a generated file dependent of the <tt>Counters.xml</tt> file. The way I prefer to do this is to actually manually add it directly into the project definition file. Open the PerformanceCounters.csproj file in your editor, locate the <tt>&lt;ItemGroup&gt;</tt> containing the <tt>Program.cs</tt> and insert this snippet:</p>
<pre>
	  &lt;Compile Include="CountersGenerated.cs"&gt;
		  &lt;AutoGen&gt;True&lt;/AutoGen&gt;
		  &lt;DependentUpon&gt;Counters.xml&lt;/DependentUpon&gt;
	  &lt;/Compile&gt;
</pre>
<p><a href="http://rusanu.com/wp-content/uploads/2009/04/editproject.png"><img src="http://rusanu.com/wp-content/uploads/2009/04/editproject.png" alt="" title="editproject" width="300" height="90" class="alignnone size-medium wp-image-327" /></a></p>
<p>Save the manually edited .csproj file and open the project normally from Visual Studio. The <tt>CountersGenerated.cs</tt> file now shows up in the Project Explorer as a generated file depending on <tt>Counters.xml</tt>:</p>
<p><a href="http://rusanu.com/wp-content/uploads/2009/04/dependentautogen.png"><img src="http://rusanu.com/wp-content/uploads/2009/04/dependentautogen.png" alt="" title="dependentautogen" width="255" height="173" class="alignnone size-medium wp-image-329" /></a></p>
<p>The warning triangle is shown because the file does not exist on the disk, but don&#8217;t worry about it as it will be generated shortly.</p>
<h3>Defining the Performance Counters</h3>
<p>We can go ahead and define the Performance Counters. The counters covering related functionality are grouped together and the groups can be single instance or multiple instance types. I also like to create a manager class that controls the management of the counters taking care of things like deployment during application setup and loading the counters when application starts. So my choice is for a XML like <tt>manager/group/counter</tt>. Some attributes are needed for the code generation XSLT transformation to know what class names and namespaces to use and some attributes are Performance Counters specific, like the counter types and bases for average counters. For start we&#8217;ll create a simple counter and a group:</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">&lt;?</span><span style="color:Maroon">xml</span><span style="color:Blue">&nbsp;</span><span style="color:Red">version</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">1.0</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;</span><span style="color:Red">encoding</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">utf-8</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;?&gt;
&lt;</span><span style="color:Maroon">manager</span><span style="color:Blue">&nbsp;</span><span style="color:Red">xmlns</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">http://rusanu.com/Schemas/PerformanceCounters/v1.0</span><span style="color:Black">"
</span><span style="color:Blue">		</span><span style="color:Red">clrname</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">PerformanceCountersManager</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;</span><span style="color:Red">namespace</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">PerformanceCounters</span><span style="color:Black">"</span><span style="color:Blue">&gt;
	&lt;</span><span style="color:Maroon">group</span><span style="color:Blue">&nbsp;</span><span style="color:Red">name</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">My&nbsp;Counters</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;</span><span style="color:Red">clrname</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">MyCounters</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;</span><span style="color:Red">type</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">SingleInstance</span><span style="color:Black">"</span><span style="color:Blue">&gt;
		&lt;</span><span style="color:Maroon">description</span><span style="color:Blue">&gt;</span><span style="color:Black">Demo&nbsp;Counters&nbsp;for&nbsp;XSLT&nbsp;code&nbsp;generation&nbsp;project</span><span style="color:Blue">&lt;/</span><span style="color:Maroon">description</span><span style="color:Blue">&gt;
		&lt;</span><span style="color:Maroon">counter</span><span style="color:Blue">&nbsp;</span><span style="color:Red">name</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">My&nbsp;Count</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;</span><span style="color:Red">type</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">NumberOfItems32</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;</span>
                                      <span style="color:Red">clrname</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">MyCount</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;</span><span style="color:Red">hasbase</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">No</span><span style="color:Black">"</span><span style="color:Blue">&gt;</span><span style="color:Black">Demo&nbsp;Counter</span><span style="color:Blue">&lt;/</span><span style="color:Maroon">counter</span><span style="color:Blue">&gt;
	&lt;/</span><span style="color:Maroon">group</span><span style="color:Blue">&gt;
&lt;/</span><span style="color:Maroon">manager</span><span style="color:Blue">&gt;</span>
</pre>
<p>This definition will create:</p>
<ul>
<li>A manager class named <tt>PerformanceCounters.PerformanceCountersManager</tt>.</li>
<li>A performance counters category named <tt>My Group</tt>.</li>
<li>A C# class named <tt>MyGroup</tt> for the above performance counters category.</li>
<li>A performance counter named <tt>My Count</tt>.</li>
<li>A C# method named <tt>IncrementMyCount</tt> to increment the counter above.</li>
</ul>
<p>Now we need to set in place the actual XSLT transformation that will generate code as we desire.</p>
<h3>The XSTL transformation</h3>
<p>We want our XSLT transformation to generate C# code, so we&#8217;re going to start by a stylesheet that will generate the skeleton of a C# file: the using directives and some comment to warn the user that the file is a generated file and should not be manually modified. Also our XSLT transformation is directed to generate an <tt>text</tt> output as opposed to the default <tt>XML</tt> output:</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">&lt;?</span><span style="color:Maroon">xml</span><span style="color:Blue">&nbsp;</span><span style="color:Red">version</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">1.0</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;</span><span style="color:Red">encoding</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">UTF-8</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;?&gt;
&lt;</span><span style="color:Teal">xsl:stylesheet</span><span style="color:Blue">&nbsp;</span><span style="color:Red">version</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">1.0</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;
	</span><span style="color:Red">xmlns:xsl</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">http://www.w3.org/1999/XSL/Transform</span><span style="color:Black">"
</span><span style="color:Blue">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:Red">xmlns:pc</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">http://rusanu.com/Schemas/PerformanceCounters/v1.0</span><span style="color:Black">"</span><span style="color:Blue">&gt;
	&lt;</span><span style="color:Teal">xsl:output</span><span style="color:Blue">&nbsp;</span><span style="color:Red">method</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">text</span><span style="color:Black">"</span><span style="color:Blue">&nbsp;</span><span style="color:Red">encoding</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">utf-8</span><span style="color:Black">"</span><span style="color:Blue">/&gt;
	&lt;</span><span style="color:Teal">xsl:template</span><span style="color:Blue">&nbsp;</span><span style="color:Red">match</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">/</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">/*&nbsp;This&nbsp;file&nbsp;was&nbsp;automatically&nbsp;generated&nbsp;during&nbsp;project&nbsp;build.
Do&nbsp;not&nbsp;manually&nbsp;modify&nbsp;this&nbsp;file&nbsp;as&nbsp;updates&nbsp;will&nbsp;be&nbsp;lost.*/
using&nbsp;System;
using&nbsp;System.Diagnostics;

</span><span style="color:Blue">		&lt;</span><span style="color:Teal">xsl:apply-templates</span><span style="color:Blue">/&gt;
&lt;/</span><span style="color:Teal">xsl:template</span><span style="color:Blue">&gt;
&lt;/</span><span style="color:Teal">xsl:stylesheet</span><span style="color:Blue">&gt;</span>
</pre>
<p>This skeleton XSLT can be used for any C# code generation, provided of course you modify the appropriate <tt>using</tt> directives.</p>
<p>Next we should add a template to generate our C# code. How one writes XSLT transformation templates is largely a matter of style and once you get the hang of it it quickly becomes a second nature. XSLT is a fully fledged programming language of its own and you can start thinking at templates in terms of procedures and functions. At least I know I do :). Our XSLT template should do the following:</p>
<ul>
<li>Identify the manager defined in the XML file and generate a C# class for it.</li>
<li>Generate a class for each performance counter category.</li>
<li>Generate a method for each individual performance counter value to read and increment the counter.</li>
<li>Hook up the manager class to provide methods for deploying and deleting the performance counter categories.</li>
<li>Hook up the manager class with an instance of each performance counter category class.</li>
<li>Hook up the manager to provide loading of the default instance of the performance counters category classes.</li>
</ul>
<p>Note that the above refer mostly to the SingleInstance performance counter category types. Multiple Instance category are a bitmore complex and I will cover them in my next post.</p>
<pre>
<span style="color: Black"></span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:template</span><span style="color:Blue">&nbsp;</span><span style="color:Red">match</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">/pc:manager</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">namespace&nbsp;</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@namespace</span><span style="color:Black">"</span><span style="color:Blue">/&gt;
</span><span style="color:Black">{
public&nbsp;partial&nbsp;class&nbsp;</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;
</span><span style="color:Black">{
</span><span style="color:Blue">	&lt;</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">./pc:group[@type='SingleInstance']</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">	private&nbsp;static&nbsp;</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">&nbsp;__</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">&nbsp;=&nbsp;
		new&nbsp;</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">();
	public&nbsp;static&nbsp;</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">&nbsp;Default</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">&nbsp;{
		get&nbsp;{return&nbsp;__</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">;&nbsp;}&nbsp;
	}
</span><span style="color:Blue">	&lt;/</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&gt;

</span><span style="color:Black">	public&nbsp;static&nbsp;void&nbsp;LoadSingleInstance(bool&nbsp;fReadOnly)
	{
</span><span style="color:Blue">		&lt;</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">./pc:group[@type='SingleInstance']</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">		__</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">.Load(fReadOnly);
</span><span style="color:Blue">		&lt;/</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&gt;
</span><span style="color:Black">	}		
</span><span style="color:Blue">	
</span><span style="color:Black">	public&nbsp;static&nbsp;void&nbsp;DeleteSingleInstance()
	{
</span><span style="color:Blue">		&lt;</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">./pc:group[@type='SingleInstance']</span><span style="color:Black">"</span><span style="color:Blue">&gt;
		&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">.Delete();
</span><span style="color:Blue">		&lt;/</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&gt;
</span><span style="color:Black">	}
</span><span style="color:Blue">	
</span><span style="color:Black">	public&nbsp;static&nbsp;void&nbsp;CreateSingleInstance()
	{
</span><span style="color:Blue">		&lt;</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">./pc:group[@type='SingleInstance']</span><span style="color:Black">"</span><span style="color:Blue">&gt;
		&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">.Create();
</span><span style="color:Blue">		&lt;/</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&gt;
</span><span style="color:Black">	}	
}

</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">./pc:group</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">public&nbsp;partial&nbsp;class&nbsp;</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;
</span><span style="color:Black">{
	private&nbsp;const&nbsp;string&nbsp;__description&nbsp;=&nbsp;
		@"</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">./pc:description/text()</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">";
	private&nbsp;const&nbsp;string&nbsp;__category&nbsp;=&nbsp;
		@"</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@name</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">";
</span><span style="color:Blue">	
	&lt;</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">./pc:counter</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">&nbsp;	private&nbsp;PerformanceCounter&nbsp;_</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">;
</span><span style="color:Blue">	&lt;</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&nbsp;</span><span style="color:Red">test</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@hasbase='Yes'</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">	private&nbsp;PerformanceCounter&nbsp;_</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">Base;
</span><span style="color:Blue">	&lt;/</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&gt;

</span><span style="color:Black">	public&nbsp;void&nbsp;Increment</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">(long&nbsp;value)
	{
		_</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">.IncrementBy(value);
</span><span style="color:Blue">		&lt;</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&nbsp;</span><span style="color:Red">test</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@hasbase='Yes'</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">		_</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">Base.IncrementBy(1);
</span><span style="color:Blue">		&lt;/</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&gt;
</span><span style="color:Black">	}
</span><span style="color:Blue">	
</span><span style="color:Black">	public&nbsp;float&nbsp;</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">&nbsp;{
		get&nbsp;{&nbsp;return&nbsp;_</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">.NextValue();&nbsp;}&nbsp;
	}
</span><span style="color:Blue">	
	&lt;/</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&gt;

	&lt;</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&nbsp;</span><span style="color:Red">test</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@type='MultipleInstance'</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">&nbsp;&nbsp;&nbsp;internal&nbsp;void&nbsp;Load(bool&nbsp;fReadOnly,&nbsp;string&nbsp;instanceName)&nbsp;{
</span><span style="color:Blue">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">./pc:counter</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">&nbsp;=&nbsp;new&nbsp;PerformanceCounter(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__category,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@"</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@name</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instanceName,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fReadOnly);
</span><span style="color:Blue">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&nbsp;</span><span style="color:Red">test</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@hasbase='Yes'</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">Base&nbsp;=&nbsp;new&nbsp;PerformanceCounter(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__category,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@"</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@name</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">Base",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instanceName,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fReadOnly);
</span><span style="color:Blue">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&gt;
</span><span style="color:Black">&nbsp;&nbsp;&nbsp;&nbsp;}
</span><span style="color:Blue">	&lt;/</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&gt;

	&lt;</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&nbsp;</span><span style="color:Red">test</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@type='SingleInstance'</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">&nbsp;&nbsp;&nbsp;&nbsp;internal&nbsp;void&nbsp;Load&nbsp;(bool&nbsp;fReadOnly)
&nbsp;&nbsp;&nbsp;&nbsp;{
</span><span style="color:Blue">&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">./pc:counter</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">&nbsp;=&nbsp;new&nbsp;PerformanceCounter(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__category,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@"</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@name</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fReadOnly);
</span><span style="color:Blue">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&nbsp;</span><span style="color:Red">test</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@hasbase='Yes'</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@clrname</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">Base&nbsp;=&nbsp;new&nbsp;PerformanceCounter(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__category,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@"</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@name</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">Base",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fReadOnly);
</span><span style="color:Blue">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&gt;
</span><span style="color:Black">&nbsp;&nbsp;&nbsp;&nbsp;}	
</span><span style="color:Blue">	&lt;/</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&gt;

</span><span style="color:Black">&nbsp;&nbsp;&nbsp;internal&nbsp;static&nbsp;bool&nbsp;Exists
&nbsp;&nbsp;&nbsp;&nbsp;{
		get&nbsp;{return&nbsp;PerformanceCounterCategory.Exists(__category);}
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;internal&nbsp;static&nbsp;void&nbsp;Delete&nbsp;()
&nbsp;&nbsp;&nbsp;&nbsp;{
		PerformanceCounterCategory.Delete(__category);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;Create&nbsp;()
&nbsp;&nbsp;&nbsp;&nbsp;{
		CounterCreationDataCollection&nbsp;ccdc&nbsp;=&nbsp;new&nbsp;CounterCreationDataCollection();
</span><span style="color:Blue">		&lt;</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">./pc:counter</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">		ccdc.Add(new&nbsp;CounterCreationData&nbsp;(
			@"</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@name</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">",
			@"</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">.</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">",
			PerformanceCounterType.</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@type</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">));
</span><span style="color:Blue">		&lt;</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&nbsp;</span><span style="color:Red">test</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@hasbase='Yes'</span><span style="color:Black">"</span><span style="color:Blue">&gt;
</span><span style="color:Black">		ccdc.Add(new&nbsp;CounterCreationData&nbsp;(
			@"</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@name</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">Base",
			@"Base&nbsp;for&nbsp;</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">.</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">",
			PerformanceCounterType.AverageBase));
</span><span style="color:Blue">		&lt;/</span><span style="color:Teal">xsl:if</span><span style="color:Blue">&gt;
		&lt;/</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&gt;
</span><span style="color:Black">		PerformanceCounterCategory.Create(
			__category,
			__description,
			PerformanceCounterCategoryType.</span><span style="color:Blue">&lt;</span><span style="color:Teal">xsl:value-of</span><span style="color:Blue">&nbsp;</span><span style="color:Red">select</span><span style="color:Blue">=</span><span style="color:Black">"</span><span style="color:Blue">@type</span><span style="color:Black">"</span><span style="color:Blue">/&gt;</span><span style="color:Black">,
			ccdc);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span><span style="color:Blue">&nbsp;
&lt;/</span><span style="color:Teal">xsl:for-each</span><span style="color:Blue">&gt;
</span><span style="color:Black">}
</span><span style="color:Blue">&lt;/</span><span style="color:Teal">xsl:template</span><span style="color:Blue">&gt;</span>
</pre>
<h3>The generate code</h3>
<p>If we go ahead and build the project. It will run our XSLT transformation over the definition XML file and the result will be saved in the <tt>CountersGenerated.cs</tt> file:</p>
<pre>
<span style="color: Black"></span><span style="color:Green">/*&nbsp;This&nbsp;file&nbsp;was&nbsp;automatically&nbsp;generated&nbsp;during&nbsp;project&nbsp;build.
Do&nbsp;not&nbsp;manually&nbsp;modify&nbsp;this&nbsp;file&nbsp;as&nbsp;updates&nbsp;will&nbsp;be&nbsp;lost.*/
</span><span style="color:Blue">using</span><span style="color:Black">&nbsp;System;
</span><span style="color:Blue">using</span><span style="color:Black">&nbsp;System.Diagnostics;


</span><span style="color:Blue">namespace</span><span style="color:Black">&nbsp;PerformanceCounters
{
</span><span style="color:Blue">public</span><span style="color:Black">&nbsp;</span><span style="color:Blue">partial</span><span style="color:Black">&nbsp;</span><span style="color:Blue">class</span><span style="color:Black">&nbsp;</span><span style="color:Teal">PerformanceCountersManager
</span><span style="color:Black">{

	</span><span style="color:Blue">private</span><span style="color:Black">&nbsp;</span><span style="color:Blue">static</span><span style="color:Black">&nbsp;</span><span style="color:Teal">MyCounters</span><span style="color:Black">&nbsp;__MyCounters&nbsp;=&nbsp;
		</span><span style="color:Blue">new</span><span style="color:Black">&nbsp;</span><span style="color:Teal">MyCounters</span><span style="color:Black">();
	</span><span style="color:Blue">public</span><span style="color:Black">&nbsp;</span><span style="color:Blue">static</span><span style="color:Black">&nbsp;</span><span style="color:Teal">MyCounters</span><span style="color:Black">&nbsp;DefaultMyCounters&nbsp;{
		</span><span style="color:Blue">get</span><span style="color:Black">&nbsp;{</span><span style="color:Blue">return</span><span style="color:Black">&nbsp;__MyCounters;&nbsp;}&nbsp;
	}


	</span><span style="color:Blue">public</span><span style="color:Black">&nbsp;</span><span style="color:Blue">static</span><span style="color:Black">&nbsp;</span><span style="color:Blue">void</span><span style="color:Black">&nbsp;LoadSingleInstance(</span><span style="color:Blue">bool</span><span style="color:Black">&nbsp;fReadOnly)
	{

		__MyCounters.Load(fReadOnly);

	}		

	</span><span style="color:Blue">public</span><span style="color:Black">&nbsp;</span><span style="color:Blue">static</span><span style="color:Black">&nbsp;</span><span style="color:Blue">void</span><span style="color:Black">&nbsp;DeleteSingleInstance()
	{
		</span><span style="color:Teal">MyCounters</span><span style="color:Black">.Delete();

	}

	</span><span style="color:Blue">public</span><span style="color:Black">&nbsp;</span><span style="color:Blue">static</span><span style="color:Black">&nbsp;</span><span style="color:Blue">void</span><span style="color:Black">&nbsp;CreateSingleInstance()
	{
		</span><span style="color:Teal">MyCounters</span><span style="color:Black">.Create();

	}	
}


</span><span style="color:Blue">public</span><span style="color:Black">&nbsp;</span><span style="color:Blue">partial</span><span style="color:Black">&nbsp;</span><span style="color:Blue">class</span><span style="color:Black">&nbsp;</span><span style="color:Teal">MyCounters
</span><span style="color:Black">{
	</span><span style="color:Blue">private</span><span style="color:Black">&nbsp;</span><span style="color:Blue">const</span><span style="color:Black">&nbsp;</span><span style="color:Blue">string</span><span style="color:Black">&nbsp;__description&nbsp;=&nbsp;
		</span><span style="color:Maroon">@"Demo&nbsp;Counters&nbsp;for&nbsp;XSLT&nbsp;code&nbsp;generation&nbsp;project"</span><span style="color:Black">;
	</span><span style="color:Blue">private</span><span style="color:Black">&nbsp;</span><span style="color:Blue">const</span><span style="color:Black">&nbsp;</span><span style="color:Blue">string</span><span style="color:Black">&nbsp;__category&nbsp;=&nbsp;
		</span><span style="color:Maroon">@"My&nbsp;Counters"</span><span style="color:Black">;


&nbsp;	</span><span style="color:Blue">private</span><span style="color:Black">&nbsp;</span><span style="color:Teal">PerformanceCounter</span><span style="color:Black">&nbsp;_MyCount;


	</span><span style="color:Blue">public</span><span style="color:Black">&nbsp;</span><span style="color:Blue">void</span><span style="color:Black">&nbsp;IncrementMyCount(</span><span style="color:Blue">long</span><span style="color:Black">&nbsp;value)
	{
		_MyCount.IncrementBy(value);

	}

	</span><span style="color:Blue">public</span><span style="color:Black">&nbsp;</span><span style="color:Blue">float</span><span style="color:Black">&nbsp;MyCount&nbsp;{
		</span><span style="color:Blue">get</span><span style="color:Black">&nbsp;{&nbsp;</span><span style="color:Blue">return</span><span style="color:Black">&nbsp;_MyCount.NextValue();&nbsp;}&nbsp;
	}


&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:Blue">internal</span><span style="color:Black">&nbsp;</span><span style="color:Blue">void</span><span style="color:Black">&nbsp;Load&nbsp;(</span><span style="color:Blue">bool</span><span style="color:Black">&nbsp;fReadOnly)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_MyCount&nbsp;=&nbsp;</span><span style="color:Blue">new</span><span style="color:Black">&nbsp;</span><span style="color:Teal">PerformanceCounter</span><span style="color:Black">(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__category,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:Maroon">@"My&nbsp;Count"</span><span style="color:Black">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fReadOnly);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}	


&nbsp;&nbsp;&nbsp;</span><span style="color:Blue">internal</span><span style="color:Black">&nbsp;</span><span style="color:Blue">static</span><span style="color:Black">&nbsp;</span><span style="color:Blue">bool</span><span style="color:Black">&nbsp;Exists
&nbsp;&nbsp;&nbsp;&nbsp;{
		</span><span style="color:Blue">get</span><span style="color:Black">&nbsp;{</span><span style="color:Blue">return</span><span style="color:Black">&nbsp;</span><span style="color:Teal">PerformanceCounterCategory</span><span style="color:Black">.Exists(__category);}
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:Blue">internal</span><span style="color:Black">&nbsp;</span><span style="color:Blue">static</span><span style="color:Black">&nbsp;</span><span style="color:Blue">void</span><span style="color:Black">&nbsp;Delete&nbsp;()
&nbsp;&nbsp;&nbsp;&nbsp;{
		</span><span style="color:Teal">PerformanceCounterCategory</span><span style="color:Black">.Delete(__category);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:Blue">public</span><span style="color:Black">&nbsp;</span><span style="color:Blue">static</span><span style="color:Black">&nbsp;</span><span style="color:Blue">void</span><span style="color:Black">&nbsp;Create&nbsp;()
&nbsp;&nbsp;&nbsp;&nbsp;{
		</span><span style="color:Teal">CounterCreationDataCollection</span><span style="color:Black">&nbsp;ccdc&nbsp;=&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:Blue">new</span><span style="color:Black">&nbsp;</span><span style="color:Teal">CounterCreationDataCollection</span><span style="color:Black">();

		ccdc.Add(</span><span style="color:Blue">new</span><span style="color:Black">&nbsp;</span><span style="color:Teal">CounterCreationData</span><span style="color:Black">&nbsp;(
			</span><span style="color:Maroon">@"My&nbsp;Count"</span><span style="color:Black">,
			</span><span style="color:Maroon">@"Demo&nbsp;Counter"</span><span style="color:Black">,
			</span><span style="color:Teal">PerformanceCounterType</span><span style="color:Black">.NumberOfItems32));

		</span><span style="color:Teal">PerformanceCounterCategory</span><span style="color:Black">.Create(
			__category,
			__description,
			</span><span style="color:Teal">PerformanceCounterCategoryType</span><span style="color:Black">.SingleInstance,
			ccdc);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
&nbsp;

}
</span>
</pre>
<p>So what did we achieve? We could had written this code manually in about 5 minutes. But the big advantage comes as we add more counters to our XML definition file. We could expand it to have 10 categories with 15 counters each and it would still generate all the needed counters. And refactoring the code is a breeze. Don&#8217;t like how the counters are created? Just change the XSLT stylesheet and rebuild the project, all the performance counters creation code wil match your new preference.</p>
<p>In a future post I will cover how to use the performance counters in your projects, how to work with multiple instance counters and how to deal with the pesky <tt>AvgTimer32</tt> counter type.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2009/04/11/using-xslt-to-generate-performance-counters-code/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Service Broker Whitepaper on MSDN: the 150 trick</title>
		<link>http://rusanu.com/2009/03/25/service-broker-whitepaper-on-msdn-the-150-trick/</link>
		<comments>http://rusanu.com/2009/03/25/service-broker-whitepaper-on-msdn-the-150-trick/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 02:24:59 +0000</pubDate>
		<dc:creator>remus</dc:creator>
		
		<category><![CDATA[Announcements]]></category>

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

		<category><![CDATA[service broker]]></category>

		<category><![CDATA[white paper]]></category>

		<guid isPermaLink="false">http://rusanu.com/?p=305</guid>
		<description><![CDATA[Copyright Remus Rusanu 2009. Visit the original article at http://rusanu.com/2009/03/25/service-broker-whitepaper-on-msdn-the-150-trick/&#160;&#160;A new SQL Customer Advisory Team whitepaper was published recently: Service Broker: Performance and Scalability Techniques authored by Michael Thomassy.
The whitepaper documents the experience of a test done in Microsoft labs that measured the message throughput attainable between three initiators pushing data to a target. This [...]]]></description>
			<content:encoded><![CDATA[<p>Copyright Remus Rusanu 2009. Visit the original article at <a href="http://rusanu.com/2009/03/25/service-broker-whitepaper-on-msdn-the-150-trick/">http://rusanu.com/2009/03/25/service-broker-whitepaper-on-msdn-the-150-trick/</a></p>&nbsp;&nbsp;<br><p>A new <a href="http://sqlcat.com" target="_blank">SQL Customer Advisory Team</a> whitepaper was published recently: <a href="http://msdn.microsoft.com/en-us/library/dd576261.aspx" target="_blank">Service Broker: Performance and Scalability Techniques</a> authored by Michael Thomassy.</p>
<p>The whitepaper documents the experience of a test done in Microsoft labs that measured the message throughput attainable between three initiators pushing data to a target. This scenario resembles a high scale ETL case. The test was able to obtain a rate a nearly 18000 messages per second, which is a rate that can satisfy most high load OLTP environments. To obtain this rate Michael and his team had to overcome the high contention around updates of the dialogs system tables. He presents a very interesting trick: create 149 dialogs that remain unused and only use every 150th. This way the updates done on the system tables occur on different pages and the high PAGELATCH contention on the page containing the dialog metadata during SEND is eliminated. A very clever trick indeed. But this is a typical OLTP insert/update trick and that is the very point of the whitetpaper: that typical OLTP techniques can and <strong>should</strong> be applied to Service Broker performance tuning.</p>
]]></content:encoded>
			<wfw:commentRss>http://rusanu.com/2009/03/25/service-broker-whitepaper-on-msdn-the-150-trick/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
