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

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 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

Share

About the Author

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

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