defaultautoflusheventlistener.cs

来自「NHibernate NET开发者所需的」· CS 代码 · 共 72 行

CS
72
字号
using System;
using log4net;
using NHibernate.Engine;

namespace NHibernate.Event.Default
{
	/// <summary> 
	/// Defines the default flush event listeners used by hibernate for 
	/// flushing session state in response to generated auto-flush events. 
	/// </summary>
	[Serializable]
	public class DefaultAutoFlushEventListener : AbstractFlushingEventListener, IAutoFlushEventListener
	{
		private static readonly ILog log = LogManager.GetLogger(typeof(DefaultAutoFlushEventListener));

		#region IAutoFlushEventListener Members

		/// <summary>
		/// Handle the given auto-flush event.
		/// </summary>
		/// <param name="event">The auto-flush event to be handled.</param>
		public virtual void OnAutoFlush(AutoFlushEvent @event)
		{
			IEventSource source = @event.Session;

			if (FlushMightBeNeeded(source))
			{
				int oldSize = source.ActionQueue.CollectionRemovalsCount;

				FlushEverythingToExecutions(@event);

				if (FlushIsReallyNeeded(@event, source))
				{
					if (log.IsDebugEnabled)
						log.Debug("Need to execute flush");

					PerformExecutions(source);
					PostFlush(source);
					// note: performExecutions() clears all collectionXxxxtion 
					// collections (the collection actions) in the session

					if (source.Factory.Statistics.IsStatisticsEnabled)
					{
						source.Factory.StatisticsImplementor.Flush();
					}
				}
				else
				{

					if (log.IsDebugEnabled)
						log.Debug("Dont need to execute flush");
					source.ActionQueue.ClearFromFlushNeededCheck(oldSize);
				}

				@event.FlushRequired = FlushIsReallyNeeded(@event, source);
			}
		}

		#endregion

		private bool FlushIsReallyNeeded(AutoFlushEvent @event, IEventSource source)
		{
			return source.ActionQueue.AreTablesToBeUpdated(@event.QuerySpaces) || ((ISessionImplementor)source).FlushMode == FlushMode.Always;
		}

		private bool FlushMightBeNeeded(ISessionImplementor source)
		{
			return !(source.FlushMode < FlushMode.Auto) && source.DontFlushFromFind == 0 && source.PersistenceContext.HasNonReadOnlyEntities;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?