Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

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

, 2 Feb 2006
Shows how to use NHibernate in a COM+ distributed transaction.
complusnhibernatetest.zip
Castle.DynamicProxy.dll
ComPlusNHibernateTest
CPN.snk
CPNTestHarness
Iesi.Collections.dll
log4net.dll
NHibernate.dll
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

Share

About the Author

Dustin Metzgar
Software Developer
United States United States
No Biography provided
Follow on   Twitter

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 2 Feb 2006
Article Copyright 2006 by Dustin Metzgar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid