For my data access unit tests, I am a huge fan of the new System.Transactions namespace in .NET 2.0. Basically I can define my unit tests like this:
using System;
using NUnit.Framework;
using System.Transactions;
[TestFixture]
public class TestClass1
{
private TransactionScope _tx;
[Setup]
public void SetUp()
_tx = new TransactionScope();
}
[TearDown]
public void TearDown()
_tx.Dispose();
[Test]
public void TestSaveSomeData()
//some code that inserts data into the database
Without the TransactionScope above, we would end up with a bunch of junk data in the database.
What’s great about this method, is that – from the bottom layer – I can start a transaction, do all kinds of inserts/updates/deletes and even select from that data, and then roll the entire thing back when I’m done. (Even in the event of failure).
What’s not so nice is that TransactionScope uses MSDTC (Microsoft Distributed Transaction Coordinator) behind the scenes. This is fine locally as long as you have the service running on your machine. But what if you try to do this on a database on another server?
In my case I was connecting to a Windows 2000 Server on the local network. The MSDTC handshake was failing. This was due to security settings built into Windows XP SP2.
I received enourmous help from Florin Lazar’s blog post: http://blogs.msdn.com/florinlazar/archive/2004/06/18/159127.aspx
I opened up Administrative Tools -> Component Services , right-clicked on My Computer and selected Properties. Under the MSDTC tab, I clicked Security. Then I enabled Inbound and Outbound connections, and set the authentication to “No Authentication Required”. That last part is kind of confusing, but basically it’s required for pre-XP/2003 machines, such as NT or Windows 2000.
Now my transactions are running again.
I'm Ben Scheirman. I am a .NET software developer with a strong interest in agility. I work as a Principal Consultant with Sogeti.
Read more here.
email me
Ads by The Lounge
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.