Dennis van der Stelt

The most votes generally drown out the best votes

Community

News

  • Meet me at PDC08

Email Notifications

I read...

I Use...

Tags

Recent Posts

Archives

March 2006 - Posts

Rad Race 2006 : Results

After a month we finally got to hear the results and at what place we finished in the Rad Race Alex Thissen and I competed in. I spoke about the Rad Race earlier right here and what my experience was. I had no idea what place we would end, and I'm very pleased to see us finish at 4th place!

Unfortunatly I could not attend the 'ceremony', because I had to give a training this week. But here's the info from Alex his blog:

  1. CrossmarX
  2. Transfer Solutions: Ruby team
  3. Servoy
  4. Class-A
  5. Xebia
  6. Transfer Solutions: Oracle team

Congrats to the CrossmarX team. We hope to see you again next year, because we're really counting on being there again. What's trange is that the Magic Hands team didn't do so well this year, while they were last year's winners.

By the way, I was just reading about the VB.NET team (Dutch) of previous year, where they wrote that the frameworks are more important then Visual Studio and .NET itself. I hope we proved them wrong, because we 'only' used LLBLGen Pro and nothing else but Visual Studio 2005 and SQL-Server. We were totally prepared for WinForms, but not for WebForms, so we had to switch the first hour of the event and create everything from scratch, like login, registering, masterpages, user-interface, etc.

System.Transactions : Promotable Enlistment
In two previous posts, I told how great the TransactionScope of the System.Transactions namespace is. And while preparing another post, I tried to prove some points via example code. But for some reason, the example code would not do what I wanted it to. Let me explain from the beginning.
 
As said, your transaction can enlist within the TransactionScope, if some conditions are met. The first one is that the resource manager your transaction is using, supports single phase transactions. SQL-Server 2005 does, but SQL-Server 2000 doesn’t. This means transactions in SQL2000 are always MSDTC transactions, when using the TransactionScope. Another scenario is when your transaction spreads across multiple app domains. The third scenario is when another durable resource manager shows up in the same transaction.
 
In MSDN there’s a great example for this. I’ve come up with my own example, for which we need the following method, which I’ll use in the second example as well.
 

private static void InsertUser(string loginName, string password, SqlConnection conn)

{

  SqlCommand cmd = new SqlCommand("Insert into Users (LoginName, Password) Values (@LoginName, @Password)", conn);

 

  cmd.Parameters.AddWithValue("@LoginName", loginName);

  cmd.Parameters.AddWithValue("@Password", password);

 

  conn.Open();

  cmd.ExecuteScalar();

}

 
As you can see, the above method tries to insert a single user into our database. Now let’s have a look at the following code.
 
 

public static void ExecuteTest()

{

  using (TransactionScope scope = new TransactionScope())

  {

    // Open first connection

    using (SqlConnection conn1 = new SqlConnection(ConnectionString))

    {

      InsertUser("dennis", "pa$$word", conn1);

      Console.WriteLine("DistributedID : {0}", System.Transactions.Transaction.Current.TransactionInformation.DistributedIdentifier);

 

      // Open second connection

      using (SqlConnection conn2 = new SqlConnection(ConnectionString))

      {

        InsertUser("anko", "pa$$word", conn2);

        Console.WriteLine("DistributedID : {0}", System.Transactions.Transaction.Current.TransactionInformation.DistributedIdentifier);

      }

    }

  }

  Console.Write("Press enter...");

  Console.ReadLine();

}

 
To tell if we’re inside a MSDTC transaction, we’re looking at the DistributedIdentifier inside the transaction, courtesy of Sahil Malik. When this property is filled with anything but all zero’s, we know we’re inside a distributed transaction. For this example to work, you’ll have to enable the MSDTC using “net start msdtc” on your computer/server.
 
When we look at the example above, it’s easy to see that a second connection is opened inside the codeblock where we’ve opened the first connection. The first is still open, so another durable resource manager shows up and the transaction is promoted to an MSDTC transaction. Remember that this is an actual MSDN example.
 
As I was reading this, I immediately presumed the following block of code would actually work within the LTM.
 

public static void ExecuteTest()

{

  using (TransactionScope scope = new TransactionScope())

  {

    // Open first connection

    using (SqlConnection conn1 = new SqlConnection(ConnectionString))

    {

      InsertUser("dennis", "pa$$word", conn1);

      Console.WriteLine("DistributedID : {0}", System.Transactions.Transaction.Current.TransactionInformation.DistributedIdentifier);

    }

 

    // Open second connection       

    using (SqlConnection conn2 = new SqlConnection(ConnectionString))

    {

      InsertUser("anko", "pa$$word", conn2);

      Console.WriteLine("DistributedID : {0}", System.Transactions.Transaction.Current.TransactionInformation.DistributedIdentifier);

    }

  }

  Console.Write("Press enter...");

  Console.ReadLine();

}

 
As you can see, the first connection is opened via the using statement, which means it’s closed and disposed when that block is closed. The second connection is opened when the first one is closed. Or at least it seems so. Because what actually happens, is that the connection is kept open, for obvious reasons. The transaction hasn’t been committed yet! This also means however, that again the second durable resource manager enlists in the transaction, automatically making it an MSDTC transaction. The technology behind it is great, but the actual use of the TransactionScope suddenly fades away.
 
The question is, why doesn’t the TransactionScope notice the first connection still being open, using it for the second execution of the command? It seems I’m not the only one asking this question, because Alazel Acheson has created a ‘simple’ class to help with this exact problem. He’s calling it the ConnectionScope class, and you can use it within your own project. You can find it here.
 
Although we now have an option to bypass the shortcomings of the TransactionScope class, I’m still wondering about the real use of it. Perhaps it’s something we can enjoy in the future, but that’s not what I want. I want to use it now, and know about all possibilities, or in this case, impossibilities.
 
Table of contents
  1. An introduction
  2. Unit Testing
  3. Promotable Enlistment
Team Foundation Server shipped

Just got the word from Marc, that Team Foundation Server will ship today, according to this article on ActiveWin. Rob Caron also mentioned it.

Too bad it can't be downloaded from MSDN, we'll have to find another way to play around with it in our Lab.