Click here to Skip to main content
15,891,833 members
Articles / Programming Languages / C#

Using NHibernate in COM+ (.NET Enterprise Services) Distributed Transactions

Rate me:
Please Sign up or sign in to vote.
4.20/5 (7 votes)
2 Feb 20065 min read 47.9K   534   29  
Shows how to use NHibernate in a COM+ distributed transaction.
using System;
using NUnit.Framework;
using ComPlusNHibernateTest;

namespace CPNTestHarness
{
	/// <summary>
	/// An NUnit test harness to test that this all works.
	/// </summary>
	[TestFixture]
	public class TestHarness
	{
		public void Test01() 
		{
			Console.WriteLine("This test will start a transaction, add two objects, and end the transaction.");
			Console.WriteLine("It will then open a second transaction, delete the objects, and end.");

			TransactionController tc = new TransactionController();
			Table1 t1;
			Table2 t2;
			try 
			{
				Console.WriteLine("Beginning transaction");
				tc.BeginTransaction();

				t1 = new Table1();
				t1.Num = 1;
				t2 = new Table2();
				t2.Num = 2;

				Console.WriteLine("Saving first object");
				tc.Save(t1);
				Console.WriteLine("Saving second object");
				tc.Save(t2);
			}
			finally 
			{
				Console.WriteLine("Ending transaction");
				tc.EndTransaction();
			}

			try
			{
				Console.WriteLine("Beginning another transaction");
				tc.BeginTransaction();

				Console.WriteLine("Deleting first object");
				tc.Delete(t1);
				Console.WriteLine("Deleting second object");
				tc.Delete(t2);
			}
			finally 
			{
				Console.WriteLine("Ending transaction");
				tc.EndTransaction();
			}
			Console.WriteLine();
		}

		public void Test02() 
		{
			Console.WriteLine("This test will create one good object and attempt to delete a non-existant object.");
			Console.WriteLine("The end result is that the database table should be empty when the transaction ends.");

			TransactionController tc = new TransactionController();
			try 
			{
				Console.WriteLine("Beginning transaction");
				tc.BeginTransaction();

				Table1 t1 = new Table1();
				t1.Num = 1;
				Table2 t2 = new Table2();
				t2.Num = 2;

				Console.WriteLine("Saving first object");
				tc.Save(t1);
				Console.WriteLine("Attempting to delete second object");
				try 
				{
					tc.Delete(t2);
					Assert.Fail("We didn't catch an exception on the delete.  Something's wrong.");
				}
				catch 
				{
					Console.WriteLine("Caught an exception.  We're looking good.");
				}
			}
			finally 
			{
				Console.WriteLine("Ending transaction");
				tc.EndTransaction();
			}

			Console.WriteLine("Verifying that both tables are empty.");
			Assert.AreEqual(0, tc.GetAll(typeof(Table1)).Count);
			Assert.AreEqual(0, tc.GetAll(typeof(Table2)).Count);
			Console.WriteLine("Success!");
			Console.WriteLine();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer Microsoft
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions