Happy new Year – for the Chinese readers

February 18th was new year for the Chinese. This is the year of the pig. Apparently, this is also a special year of the pig. It’s the golden one which only comes every 60 years.


My wife is of Chinese extraction and so it’s been interesting following things over the last few days. We spent part of their New Years Eve at a Buddhist temple with her family. I have to say that was quite fascinating along with trying a few foods I’ve never seen before.


So, to any readers of Chinese extraction – happy new year!

The Elements of C# Style

Mitch Wheat recently posted about a wonderful little book called “The Elements of C# Style“. I loved reading Strunk’s “The Elements of Style” so I had to pick up a copy and read it. Mitch was spot on. This is a great little book that doesn’t take long to read and provides a great summary of good C# coding style.


We have regular discussions in the blogosphere about whether or not sets of rules on coding are a good or bad thing. My own take on this is that it is critical to distill this sort of information so that newcomers to the industry have good guidance.


In this book, Kenneth Baldwin, Andrew Gray and Trevor Misfeldt have provided solid guidance for anyone writing C# code. I presume most people who read the book would do what I did and constantly think they are stating the blindingly obvious with most of their points. However, you need to think back to how you learned those things in the first place. For most of us, that was via the school of hard knocks. There was a time when many of these things would not have been obvious to you.


I also agree with Mitch that the one main error I found in the book was the recommendation to derive custom exception classes from ApplicationException instead of Exception. Brad Abrams and co now suggest *not* doing that.


Recommended!

Improving ASP.NET Session State database performance by reducing blocking

On a recent consulting engagement, I was working with a client that had significant performance issues with their ASP.NET session state database. They had a combination of both large session state and a large number of concurrent sessions. They were regularly experiencing command timeouts on that database.


In my investigation, curiously I found that a DELETE statement was the culprit. I tracked it to the DeleteExpiredSessions stored procedure. Looking at it, it seems tame enough:


CREATE PROCEDURE DeleteExpiredSessions


AS


  DECLARE @now DATETIME


  SET @now = GETUTCDATE()


 


  DELETE ASPState..ASPStateTempSessions


  WHERE Expires < @now


 


  RETURN 0


GO


However, the problem is that as session size grows, each delete takes longer and as the number of sessions grows, this simple DELETE ends up causing substantial blocking. It was at the head of nearly every blocking chain. This proc is run every five minutes.


There is no need for this proc to do all the deletes in a single operation. I replaced it with one that does a series of individual deletes:


CREATE PROCEDURE dbo.DeleteExpiredSessions


AS


  DECLARE @now datetime


  SET @now = GETUTCDATE()


 


  CREATE TABLE #ExpiredSessions


  ( SessionID nvarchar(88) NOT NULL


      PRIMARY KEY


  )


 


  INSERT #ExpiredSessions (SessionID)


  SELECT SessionID


  FROM [ASPState_2_0].dbo.ASPStateTempSessions


  WHERE Expires < @now


 


  DECLARE SessionCursor CURSOR LOCAL FORWARD_ONLY READ_ONLY


  FOR SELECT SessionID FROM #ExpiredSessions ORDER BY CHECKSUM(NEWID())


 


  DECLARE @SessionID nvarchar(88)


 


  OPEN SessionCursor


  FETCH NEXT FROM SessionCursor INTO @SessionID


  WHILE @@FETCH_STATUS = 0 BEGIN


    DELETE FROM [ASPState_2_0].dbo.ASPStateTempSessions


      WHERE SessionID = @SessionID


    FETCH NEXT FROM SessionCursor INTO @SessionID


  END


  CLOSE SessionCursor


  DEALLOCATE SessionCursor


 


  DROP TABLE #ExpiredSessions


  RETURN 0


GO


While I’m not a fan of using cursors, the use of one here is ideal. The other less than obvious advantage of this version of the proc is that the cursor is designed to traverse the table of expired sessions in random order (CHECKSUM(NEWID())). This means that if the proc isn’t working fast enough, you can simply add another job to run another copy of the proc at the same time. If one copy happens to try to delete a row that’s already deleted by the other copy, that’s not an issue anyway.


The blocking was completely solved by replacing the proc with this version.

Want to try to help cure cancer and do interesting work?

Fellow RD, Tim Huckaby (who many would know from his presentations when he’s been in the country) has been doing some awesome demos lately of a project he’s on that’s helping in the fight to cure cancer. Heavy use of visualisation is involved. Recently, he’s been on stage with Steve Ballmer showing this and it’s getting tons of coverage.

So how does this affect you? They now want to take this to stage two and are looking for some of the brightest .NET folk on the planet to help. The work will be done remotely. Microsoft is sponsoring the project in conjunction with the Scripps Research Institute. The work would be undertaken by Interknowlogy.

They are looking for people with Winforms knowledge (preferably now WPF), Sharepoint and Office/XML.

Details of the work are mentioned on another fellow RD (Stephen Forte)’s site here: http://www.stephenforte.net/owdasblog/PermaLink.aspx?guid=81b20029-dd37-4d0c-a9f2-8e9220eae12d