Click here to Skip to main content
15,885,176 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.8K   534   29  
Shows how to use NHibernate in a COM+ distributed transaction.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.EnterpriseServices;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;

namespace ComPlusNHibernateTest
{
	/// <summary>
	/// Controls the use of <see cref="Table1"/> and <see cref="Table2"/>.
	/// </summary>
	[Transaction(TransactionOption.Supported)]
	public class OrmManager : ServicedComponent
	{
		private ISessionFactory _SessionFactory;
		
		/// <summary>
		/// Gets the session factory for this assembly.
		/// </summary>
		private ISessionFactory SessionFactory 
		{
			get 
			{
				if (_SessionFactory == null) 
				{
					Configuration cfg = new Configuration();
					cfg.Properties = GetConfigProps();
					cfg.AddAssembly(this.GetType().Assembly);
					_SessionFactory = cfg.BuildSessionFactory();
				}
				return _SessionFactory;
			}
		}

		public OrmManager()
		{
		}

		private Hashtable GetConfigProps() 
		{
			Hashtable props = new Hashtable();
			props.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
			props.Add("hibernate.dialect", "NHibernate.Dialect.MsSql2000Dialect");
			props.Add("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
			props.Add("hibernate.connection.connection_string", ConfigurationSettings.AppSettings["ConnectionString"]);
			return props;
		}

		/// <summary>
		/// This method uses reflection to enlist in the distributed transaction.
		/// </summary>
		/// <param name="conn"></param>
		private static void EnlistIfPossible(System.Data.IDbConnection conn) 
		{
			if (ContextUtil.IsInTransaction) 
			{
				MethodInfo mi = conn.GetType().GetMethod("EnlistDistributedTransaction", BindingFlags.Public | BindingFlags.Instance);
				if (mi != null) 
				{
					mi.Invoke(conn, new object[] { (System.EnterpriseServices.ITransaction)ContextUtil.Transaction });
				}
			}
		}

		public void Save(object obj) 
		{
			ISession session = SessionFactory.OpenSession();
			try 
			{
				EnlistIfPossible(session.Connection);
				session.SaveOrUpdate(obj);
				session.Flush();
				if (ContextUtil.IsInTransaction)
					ContextUtil.MyTransactionVote = TransactionVote.Commit;
			}
			catch 
			{
				if (ContextUtil.IsInTransaction)
					ContextUtil.MyTransactionVote = TransactionVote.Abort;
				throw;
			}
			finally 
			{
				if (session != null && session.IsOpen)
					session.Close();
			}
		}

		public void Delete(object obj) 
		{
			ISession session = SessionFactory.OpenSession();
			try 
			{
				EnlistIfPossible(session.Connection);
				session.Delete(obj);
				session.Flush();
				if (ContextUtil.IsInTransaction)
					ContextUtil.MyTransactionVote = TransactionVote.Commit;
			}
			catch 
			{
				if (ContextUtil.IsInTransaction)
					ContextUtil.MyTransactionVote = TransactionVote.Abort;
				throw;
			}
			finally 
			{
				if (session != null && session.IsOpen)
					session.Close();
			}
		}

		public IList GetAll(Type t) 
		{
			ISession session = SessionFactory.OpenSession();
			try 
			{
				IList lst = session.CreateCriteria(t).List();
				return lst;
			}
			finally 
			{
				if (session != null && session.IsOpen)
					session.Close();
			}
		}
	}
}

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