Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / XML

Building a Middle Tier Component using NHibernate and Spring.NET

Rate me:
Please Sign up or sign in to vote.
4.50/5 (21 votes)
10 May 2006CPOL8 min read 159K   2.4K   109  
Building a highly pluggable middle-tier component with NHibernate and Spring.Net.
#region License

/*
 * Copyright � 2002-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

#region Imports

using System;
using System.Diagnostics;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Type;
using Spring.Objects.Factory;
using log4net;
using System.Collections;
using System.Collections.Specialized;
#endregion

namespace Spring.Orm.Hibernate
{

	/// <summary>
	/// Base class for HibernateTemplate and HibernateInterceptor, defining common
	/// properties like SessionFactory and flushing behavior.
	///
	/// <p>Not intended to be used directly. See HibernateTemplate and HibernateInterceptor.</p>
	///
	/// <author> Juergen Hoeller </author>
	/// <author> Moim Hossain </author>
	/// <see cref="HibernateTemplate"/> 
	/// <seealso cref="HibernateInterceptor"/> 
	/// </summary>
	public abstract class HibernateAccessor : IInitializingObject
	{
		/// <summary>
		/// Never flush is a good strategy for read-only units of work.
		/// Hibernate will not track and look for changes in this case,
		/// avoiding any overhead of modification detection.
		/// <see cref="FlushMode"/> 
		/// </summary>
		public static int FLUSH_NEVER = 0;
		/// <summary>
		/// Automatic flushing is the default mode for a Hibernate session.
		/// A session will get flushed on transaction commit or session closing,
		/// and on certain find operations that might involve already modified
		/// instances, but not after each unit of work like with eager flushing.
		/// </summary>
		public static int FLUSH_AUTO = 1;
		/// <summary>
		/// Eager flushing leads to immediate synchronization with the database,
		/// even if in a transaction. This causes inconsistencies to show up and throw
		/// a respective exception immediately, and JDBC access code that participates
		/// in the same transaction will see the changes as the database is already
		/// aware of them then. But the drawbacks are:
		/// <ul>
		/// <li>additional communication roundtrips with the database, instead of a
		/// single batch at transaction commit;</li>
		/// <li>the fact that an actual database rollback is needed if the Hibernate
		/// transaction rolls back (due to already submitted SQL statements).</li>
		/// </ul>
		/// </summary>
		public static int FLUSH_EAGER = 2;
		/// <summary>
		/// Logger
		/// </summary>
		protected ILog logger = LogManager.GetLogger(typeof(HibernateAccessor));
		/// <summary>
		/// Session Factory Instance
		/// </summary>
		private ISessionFactory sessionFactory;
		/// <summary>
		/// The Interceptor for the Entity
		/// </summary>
		private IInterceptor entityInterceptor;
		/// <summary>
		/// The Falush Mode
		/// </summary>
		private int flushMode = FLUSH_AUTO;
		/// <summary>
		/// Get or Set the Flush Mode
		/// </summary>
		public int FlushMode
		{
			get
			{
				return flushMode;
			}
			set
			{
				flushMode = value;
			}
		}
		/// <summary>
		/// Get or Set the Interceptor
		/// </summary>
		public IInterceptor EntityInterceptor
		{
			get
			{
				return entityInterceptor;
			}
			set
			{
				entityInterceptor = value;
			}
		}
		/// <summary>
		/// Get or Set the Session Factory
		/// </summary>
		public ISessionFactory SessionFactory
		{
			get
			{
				return sessionFactory;
			}
			set
			{
				sessionFactory = value;
			}
		}

		#region IInitializingObject Members
		/// <summary>
		/// Validate the IoC Injection
		/// </summary>
		public void AfterPropertiesSet()
		{
			System.Diagnostics.Trace.Assert( (null != sessionFactory) , "Session Factory is required.");
		}

		#endregion
		/// <summary>
		/// Flush the given Hibernate session if necessary.
		/// <param name="session"> param session the current Hibernate session </param>
		/// <param name="existingTransaction"> existingTransaction if executing within an existing transaction </param>
		/// </summary>
		public void FlushIfNecessary(ISession session, bool existingTransaction) 
		{
			if ( flushMode == FLUSH_EAGER || (!existingTransaction && flushMode == FLUSH_AUTO )) 
			{
				logger.Debug("Eagerly flushing Hibernate session");
				session.Flush();
			}
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Netherlands Netherlands
Engineer Powered by the Cloud

Comments and Discussions