David Foderick's Blog - OnMaterialize()

November 2005 - Posts

Beware of NOLOCK hint

At many of the companies where I consult, use of the NOLOCK hint is ubiquitous.

In the normal situation , without NOLOCK, SQL Server places a Read lock on the rows that are read when you issue a SELECT statement. (These read locks are only held for the duration of the select statement.) Many teams have developed the habit of using the NOLOCK hint on all select statements. The NOLOCK hint tells SQL Server not to issue these read locks thus relieving SQL Server from having to administer these locks. The NOLOCK hint will look like this:

SELECT * FROM Customer WITH (NOLOCK) WHERE CustomerID=1

So far, so good. No Read locks, less overhead, more efficient server. Now here's the problem that I think most people do not realize: NOLOCK is equivalent to READUNCOMMITTED. That means that you are potentially reading dirty data! Read Uncommitted is the lowest isolation level and permits the connection to "read through" any exclusive locks held  by another transaction and read the other transaction's uncommitted data. If you use NOLOCK, you may be reading records that may never get committed to the database or you could be reading inconsistent data. This may or may not be appropriate for your application.

I suspect what is happening is that teams are running into blocking situations and instead of finding and curing the source of the blockage they simply put in a NOLOCK hint, unaware of its potentially dangerous behavior. If you don't care about data integrity, then continue to use NOLOCK. If you do care about the integrity of your application, find and fix the blockage at its source and don't sweep the problem under the rug with the promiscuous use of NOLOCK.

Introducing the Composite UI Application Block

The Composite UI Application Block (CAB) is a new UI application framework from the Patterns and Practices group that was released just this week. Similar to Web Parts for building ASP.NET web pages, CAB allows you to create pluggable modules for WinForms applications. Here are some of the highlights of the CAB:

  • Built from the ground up using version 2.0 of the .NET Framework. That means it takes full advantage of the latest features of .NET 2.0, including generics. In addition, it provides a migration path to future technologies such as Avalon and XAML.
  • A pluggable architecture built on design patterns. It makes full use of Dependency Injection, Lightweight containers (Component Model) and Services.
  • The ObjectBuilder will be the foundation for dependency injection in future version of the Enterprise Library.
  • A Declarative eventing model called the EventBroker for communicating between modules.
  • Workflow integration.

The Composite UI Application Block is a great tool to learn how to build complex UI's out of simpler plugin modules.

Getting started with the Composite UI Application Block

I've just started working with the final release of the Composite UI Application Block (CAB) and I am happy to report my first snafu.

Problem: Your module doesn't Load
Solution: Check your ModuleInit. Make sure your class that derives from ModuleInit is public.

The CAB loads modules into the shell via reflection in the ModuleLoaderService. It looks at all the modules that are configured in the ProfileCatalog and looks for the publicly available classes of the assembly. No public ModuleInit - no module gets configured. Well, guess what? When you create a new class in Visual Studio 2005 it is internal, not public! Therefore, make sure you change the ModuleInit class from internal to public.

Life is good. CAB rocks.

Things to check before sending your laptop for repair...

My well-worn laptop, a Dell D-800, has been showing signs of aging recently. I thought it had reached the breaking point when the H key became intermittant.

Feeling desparate, I popped off the key cap and removed the dust. Click. Click. Click. Still no H. In a last-ditched attempt at ressurecting it, I sucked up my lungs and blew on it real hard. What's this? Out popped a fingernail clipping! It had lodged itself between the contacts.

Now it works perfectly. Damn! Lost my excuse to buy a new laptop.

Object Relational Mapping forums

Here are my favorite places to ask questions, answer questions and generally keep up to date on the ORM world:

General Architecture Forums
ASP.NET Architecture forum. Beware! Everyone has an opinion so take everything you read on that forum with a grain of salt.

Vendor Specific Forums
Microsoft DLINQ. Should get better as people get more experience with the product.
Vanatec OpenAccess
NHibernate

And the winner is ...

Domain Driven Design messageboard. This is the place to get authoratative architectural guidance for buiding complex applications.

Schedule your defrags

If you're like me, you'll often forget to defrag your machine, forcing you to run it at some inopportune moment (like an hour before a presentation). These maintenance tasks should be configured to run with the built-in Windows task scheduler.

To set up defrag to run at a scheduled time, add a task (System Tools > Scheduled Tasks) and go through the wizard. To run defrag specify the following command line:

c:\WINDOWS\system32\defrag.exe c:\ /f

Of course, scheduler is a great way to kick off any type of scheduled process that you can image. Unix programmers will recognize the scheduler as a glorified version of the AT command.

Remove Active Directory from Windows 2003

I run Windows 2003 on my laptop because I want to be able to play around with all the server products available to delelpers: SQL Server, BizTalk and Indigo to name a few. But setting up my laptop with Active Directory (AD) was a mistake. I have since learned that many of these server products have a problem when running on a machine that has AD installed and the worst part is that the laptop runs dog slow. Here's how I got rid of it.

  1. Run dcpromo.exe. You will get a wizard-type screen. The first page tells you that removing AD will make this a standalone server. Good!That's what I want! Click next.
  2. Check the box that says this is the last domain controller in the domain. Next.
  3. Enter the admin password (don't know why it resets the password). Next.

That's it. The wizard will run for several (perhaps many) minutes and you will now be Active Directory free.

If you really need Active Directory-like services on your development box, try Active Directory Application Mode (ADAM).

Visual Studio 2005 has code performance profiling tools

This is great. VS2005 has tools out of the box to profile the performance of your application. If you have ever used RedGate ANTS then you know what I mean. The Performance Tool is available under the Tools menu. It will show you how many times your applications methods are getting executed and shows you how long they take to run.

Apparently, Enterprise Performance Tool (EPT) is part of Team System and is capable to doing sampling and fully instrumented profiling.

Now the bad part. If you try to run in Sampling mode on a virtual machine you will get the error message "VSP1454 - Sampling is not supported on Virtual Machines". In this case, you have to run in instrumented mode which is very instrusive on the execution speed of your test.

Here is a link to much more information on this tool.

Make sure to close your connection after filling a DataSet

As most of you know the DataAdapter automatically closes the database connection after filling the DataSet.

...or does it? Consider this code:

SqlConnection conn = new SqlConnection("connection string");
conn.Open();
SqlCommand cmd = new SqlCommand("select GetDate()",conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

What is the Connection.State after the Fill? If you guessed closed you would be wrong. It is true that the DataAdapter will open a connection, fill the dataset and close the connection. But that is only if the connection is closed before the Fill! If the connection is already open before the Fill, then the connection will be open after the Fill. In other words, the DataAdapter.Fill leaves the connection in the same state as it found it.

If your data access library is passing in an open connection to the DataAdapter and you are not explicitly closing it then you could be leaking a connection.

Boo! Its Halloween!

Its easy to see where Microsoft is taking the C# language. Dynamically typed languages will be all the rage in a couple years time. (Note: Dynamic typing is NOT weak typing). Anonymous delegates and types, type inferencing, lambdas and LINQ point the way to a future of dynamically typed functional-influenced languages.

An educational (and fun) way to look at the future is by looking at some of the available languages that already feature these innovations. boo looks interesting. It has a very consise Python-like syntax.

print "Hello, world!"

I like it. Anything that provides a shortcut between a thought in my brain and executable code is a good thing.

Happy future-coding.