У меня большая проблема, и она согласовывает транзакции в NHibernate.
Для начала я могу сказать, что SessionManager (поточно-ориентированный, ленивый Singleton) создает SessionFactory и предоставляет мне объекты сеанса.Встроенное управление транзакциями.
Далее, у меня есть DAL с отображениями гибернации (xml, свободно) и poco.
Служба WCF предоставляет некоторое количество poco бесплатно, и это происходит следующим образом:
public string UpdateCustomer(Customer customer, ExtraParameters extraParameters)
{
try
{
using (_daof.BeginTransaction(GetInstanceUserID(extraParameters)))
{
return _daof.GetKlantDao().UpdateCustomer(customer).SekKlant.ToString();
}
}
catch (Exception ex)
{
ServiceExceptionHandling(ex);
}
return null;
}
Как вы можете видеть, оператор using обрабатывает транзакцию и удаляет сеанс:
public CrmDaoFactory BeginTransaction(string userID)
{
CrmSettings.Instance.UserID = userID; //Not important
SessionManager.Instance.BeginTransactionOn(configKey); //Memory leak
return this;
}
Удаление выполняется с помощью шаблона удаления (деструктор):
protected virtual void Dispose(bool disposing)
{
if (disposing == true)
{
if (Marshal.GetExceptionCode() == 0)
{
SessionManager.Instance.CommitTransactionOn(configKey);
}
else
{
SessionManager.Instance.RollbackTransactionOn(configKey);
}
SessionManager.Instance.CloseSessionOn(configKey);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~CrmDaoFactory() { Dispose(false); }
Выше вы можете видеть, что при вызове dispose транзакция фиксируется (сбрасывает также сеанс) и когдапроизошла ошибка, откат.Наконец, я закрываю сессию.
Проблема в том, что когда я вызываю методы службы, я замечаю утечку памяти.Когда я цитирую строку BeginTransaction, утечки памяти не происходит.
public CrmDaoFactory BeginTransaction(string userID)
{
CrmSettings.Instance.UserID = userID;
//SessionManager.Instance.BeginTransactionOn(configKey);
return this;
}
Я вполне уверен, что все сессии закрываются после удаления, так как я отлаживал код.Для вашей информации я использую следующий код 'SessionManager':
using System;
using System.Collections;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Caching;
using NHibernate;
using NHibernate.Cache;
using System.Reflection;
namespace Cevi.Hibernate
{
/// <summary>
/// Handles creation and management of sessions and transactions. It is a singleton because
/// building the initial session factory is very expensive. Inspiration for this class came
/// from Chapter 8 of Hibernate in Action by Bauer and King. Although it is a sealed singleton
/// you can use TypeMock (http://www.typemock.com) for more flexible testing.
/// </summary>
public sealed class SessionManager
{
//private static readonly DataInterceptor _dataInterceptor = new DataInterceptor();
#region Thread-safe, lazy Singleton
/// <summary>
/// This is a thread-safe, lazy singleton. See http://www.yoda.arachsys.com/csharp/singleton.html
/// for more details about its implementation.
/// </summary>
public static SessionManager Instance
{
get
{
return Nested.SessionManager;
}
}
/// <summary>
/// Private constructor to enforce singleton
/// </summary>
private SessionManager() { }
/// <summary>
/// Assists with ensuring thread-safe, lazy singleton
/// </summary>
private class Nested
{
static Nested() { }
internal static readonly SessionManager SessionManager = new SessionManager();
}
#endregion
/*
/// <summary>
/// This method attempts to find a session factory in the <see cref="HttpRuntime.Cache" />
/// via its config file path; if it can't be found it creates a new session factory and adds
/// it the cache. Note that even though this uses HttpRuntime.Cache, it should still work in
/// Windows applications; see http://www.codeproject.com/csharp/cacheinwinformapps.asp for an
/// examination of this.
/// </summary>
/// <param name="sessionFactoryConfigPath">Path location of the factory config</param>
private ISessionFactory GetSessionFactoryFor(string sessionFactoryConfigPath)
{
if (string.IsNullOrEmpty(sessionFactoryConfigPath))
throw new ArgumentNullException("sessionFactoryConfigPath may not be null nor empty");
// Attempt to retrieve a cached SessionFactory from the HttpRuntime's cache.
ISessionFactory sessionFactory = (ISessionFactory)HttpRuntime.Cache.Get(sessionFactoryConfigPath);
// Failed to find a cached SessionFactory so make a new one.
if (sessionFactory == null)
{
if (!File.Exists(sessionFactoryConfigPath))
// It would be more appropriate to throw a more specific exception than ApplicationException
throw new ApplicationException(
"The config file at '" + sessionFactoryConfigPath + "' could not be found");
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure(sessionFactoryConfigPath);
// Now that we have our Configuration object, create a new SessionFactory
sessionFactory = cfg.BuildSessionFactory();
if (sessionFactory == null)
{
throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
}
HttpRuntime.Cache.Add(sessionFactoryConfigPath, sessionFactory, null, DateTime.Now.AddDays(7),
TimeSpan.Zero, CacheItemPriority.High, null);
}
return sessionFactory;
}
*/
/// <summary>
/// This method attempts to find a session factory in the <see cref="HttpRuntime.Cache" />
/// via its config file path; if it can't be found it creates a new session factory and adds
/// it the cache. Note that even though this uses HttpRuntime.Cache, it should still work in
/// Windows applications; see http://www.codeproject.com/csharp/cacheinwinformapps.asp for an
/// examination of this.
/// </summary>
/// <param name="sessionFactoryConfigPath">Path location of the factory config</param>
public ISessionFactory GetSessionFactoryFor(string sessionFactoryConfigPath)
{
if (string.IsNullOrEmpty(sessionFactoryConfigPath))
throw new ArgumentNullException("sessionFactoryConfigPath may not be null nor empty");
// Attempt to retrieve a cached SessionFactory from the HttpRuntime's cache.
ISessionFactory sessionFactory = (ISessionFactory)HttpRuntime.Cache.Get(sessionFactoryConfigPath);
// Failed to find a cached SessionFactory so make a new one.
if (sessionFactory == null)
{
string path = sessionFactoryConfigPath;
if (!Path.IsPathRooted(path))
// indien het geen absoluut pad is afhankelijk van de context waarin de hibernate laag gebruikt wordt het path anders aanvullen
{
if (HttpContext.Current != null)
path = HttpContext.Current.Server.MapPath("~/" + path);
else if (!string.IsNullOrEmpty(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath))
path = Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, path);
else
{
string assemblyPath = (Assembly.GetEntryAssembly()!=null)?
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) :
AppDomain.CurrentDomain.BaseDirectory;
if (!string.IsNullOrEmpty(assemblyPath))
path = Path.Combine(assemblyPath, path);
}
}
if (!File.Exists(path))
// It would be more appropriate to throw a more specific exception than ApplicationException
throw new ApplicationException(
"The config file at '" + sessionFactoryConfigPath + "' could not be found");
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure(path);
//cfg.SetInterceptor(new DataInterceptor());
// Now that we have our Configuration object, create a new SessionFactory
sessionFactory = cfg.BuildSessionFactory();
if (sessionFactory == null)
{
throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
}
HttpRuntime.Cache.Add(sessionFactoryConfigPath, sessionFactory, null, DateTime.Now.AddDays(7),
TimeSpan.Zero, CacheItemPriority.High, null);
}
return sessionFactory;
}
/// <summary>
///
/// </summary>
/// <param name="sessionFactoryConfigPath"></param>
/// <param name="interceptor"></param>
public void RegisterInterceptorOn(string sessionFactoryConfigPath, IInterceptor interceptor)
{
ISession session = (ISession)ContextSessions[sessionFactoryConfigPath];
if (session != null && session.IsOpen)
{
throw new CacheException("You cannot register an interceptor once a session has already been opened");
}
GetSessionFrom(sessionFactoryConfigPath, interceptor);
}
/// <summary>
/// </summary>
/// <param name="sessionFactoryConfigPath"></param>
/// <returns></returns>
public ISession GetSessionFrom(string sessionFactoryConfigPath)
{
return GetSessionFrom(sessionFactoryConfigPath, null);
}
private ISession GetSessionFrom(string sessionFactoryConfigPath, IInterceptor interceptor)
{
ISession session = (ISession)ContextSessions[sessionFactoryConfigPath];
if (session == null)
{
session = (interceptor != null)?
GetSessionFactoryFor(sessionFactoryConfigPath).OpenSession(interceptor) :
GetSessionFactoryFor(sessionFactoryConfigPath).OpenSession();
ContextSessions[sessionFactoryConfigPath] = session;
}
if (session == null)
// It would be more appropriate to throw a more specific exception than ApplicationException
throw new ApplicationException("session was null");
return session;
}
///<summary>
///</summary>
///<param name="sessionFactoryConfigPath"></param>
public void CloseSessionOn(string sessionFactoryConfigPath)
{
ISession session = (ISession)ContextSessions[sessionFactoryConfigPath];
if (session != null && session.IsOpen)
{
session.Close();
session.Dispose();
}
ContextSessions.Remove(sessionFactoryConfigPath);
}
///<summary>
///</summary>
///<param name="sessionFactoryConfigPath"></param>
public void BeginTransactionOn(string sessionFactoryConfigPath)
{
ITransaction transaction = (ITransaction)ContextTransactions[sessionFactoryConfigPath];
if (transaction == null)
{
transaction = GetSessionFrom(sessionFactoryConfigPath).BeginTransaction();
ContextTransactions.Add(sessionFactoryConfigPath, transaction);
}
}
///<summary>
///</summary>
///<param name="sessionFactoryConfigPath"></param>
public void CommitTransactionOn(string sessionFactoryConfigPath)
{
ITransaction transaction = (ITransaction)ContextTransactions[sessionFactoryConfigPath];
try
{
if (transaction != null && !transaction.WasCommitted && !transaction.WasRolledBack)
{
transaction.Commit();
ContextTransactions.Remove(sessionFactoryConfigPath);
transaction.Dispose();
}
}
catch (HibernateException)
{
RollbackTransactionOn(sessionFactoryConfigPath);
throw;
}
}
///<summary>
///</summary>
///<param name="sessionFactoryConfigPath"></param>
public void RollbackTransactionOn(string sessionFactoryConfigPath)
{
ITransaction transaction = (ITransaction)ContextTransactions[sessionFactoryConfigPath];
try
{
ContextTransactions.Remove(sessionFactoryConfigPath);
if (transaction != null && !transaction.WasCommitted && !transaction.WasRolledBack)
{
transaction.Rollback();
}
}
finally
{
CloseSessionOn(sessionFactoryConfigPath);
}
}
/// <summary>
/// Since multiple databases may be in use, there may be one transaction per database
/// persisted at any one time. The easiest way to store them is via a hashtable
/// with the key being tied to session factory.
/// </summary>
private static Hashtable ContextTransactions
{
get
{
if (CallContext.GetData("CONTEXT_TRANSACTIONS") == null)
{
CallContext.SetData("CONTEXT_TRANSACTIONS", new Hashtable());
}
return (Hashtable)CallContext.GetData("CONTEXT_TRANSACTIONS");
}
}
/// <summary>
/// Since multiple databases may be in use, there may be one session per database
/// persisted at any one time. The easiest way to store them is via a hashtable
/// with the key being tied to session factory.
/// </summary>
private static Hashtable ContextSessions
{
get
{
if (CallContext.GetData("CONTEXT_SESSIONS") == null)
{
CallContext.SetData("CONTEXT_SESSIONS", new Hashtable());
}
return (Hashtable)CallContext.GetData("CONTEXT_SESSIONS");
}
}
/// <summary>
/// Opens a stateless session
/// </summary>
/// <param name="sessionFactoryConfigPath"></param>
/// <returns></returns>
public IStatelessSession StatelessSession(string sessionFactoryConfigPath)
{
return GetSessionFactoryFor(sessionFactoryConfigPath).OpenStatelessSession();
}
}
}
Может кто-нибудь подсказать, что происходит?У меня заканчиваются идеи.О, кэш второго уровня и кэш запросов отключены:
<property name="cache.use_second_level_cache">false</property>
<property name="cache.use_query_cache">false</property>
Спасибо