182 lines
5.6 KiB
C#
182 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.ServiceModel;
|
|
using System.Threading;
|
|
using log4net;
|
|
using log4net.Appender;
|
|
using log4net.Layout;
|
|
using log4net.Repository.Hierarchy;
|
|
using NHibernate;
|
|
using NHibernate.Cfg;
|
|
|
|
|
|
namespace BeWo.Data
|
|
{
|
|
public class DefaultHibernateSessionManager
|
|
{
|
|
private static Dictionary<string, ISessionFactory> _SessionFactories = new Dictionary<string, ISessionFactory>();
|
|
private static object _Lock = string.Empty;
|
|
|
|
|
|
private static ISession currentSession;
|
|
|
|
public static ISession CurrentSession
|
|
{
|
|
get
|
|
{
|
|
var frame = new StackFrame(1);
|
|
var method = frame.GetMethod();
|
|
var callingMethodName = method.Name;
|
|
|
|
return currentSession ?? OpenSession();
|
|
}
|
|
set
|
|
{
|
|
currentSession = value;
|
|
}
|
|
}
|
|
|
|
public static string CurrentTenant { get; set; }
|
|
|
|
private static ISessionFactory SessionFactory
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
Monitor.Enter(_Lock);
|
|
if (!_SessionFactories.ContainsKey(CurrentTenant))
|
|
{
|
|
#if DEBUG
|
|
var hierarchy = (Hierarchy)LogManager.GetRepository();
|
|
var logger = (Logger)hierarchy.GetLogger("NHibernate.SQL");
|
|
logger.AddAppender(new TraceAppender { Layout = new SimpleLayout() });
|
|
hierarchy.Configured = true;
|
|
#endif
|
|
|
|
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
|
var splittedBaseDirectory = baseDirectory.Split('\\'); // 0 -3
|
|
var correctBaseDirectory = string.Empty;
|
|
for(var i = 0; i < 4; i++)
|
|
{
|
|
correctBaseDirectory += splittedBaseDirectory[i] + "\\";
|
|
}
|
|
|
|
var test = correctBaseDirectory;
|
|
|
|
_SessionFactories[CurrentTenant] = new Configuration().Configure(Path.Combine(correctBaseDirectory, @"Multitenancy\" + CurrentTenant + ".config")).BuildSessionFactory();
|
|
}
|
|
|
|
return _SessionFactories[CurrentTenant];
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
//String path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionFactoryError.txt");
|
|
//System.IO.File.AppendAllText(path, String.Format("{0:dd.MM.yyyy HH:mm:ss}", DateTime.Now) + "\n" + e.Message + "\n" + e.StackTrace);
|
|
Debug.WriteLine("<<<<< " + e.StackTrace + (e.InnerException != null ? e.InnerException.Message : string.Empty));
|
|
throw new FaultException(e.StackTrace + e.InnerException != null ? e.InnerException.Message : string.Empty);
|
|
}
|
|
finally
|
|
{
|
|
Monitor.Exit(_Lock);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static ISessionFactory RebuildSessionFactory()
|
|
{
|
|
ResetSessionFactory(CurrentTenant);
|
|
|
|
return SessionFactory;
|
|
}
|
|
|
|
public static void ResetAllSessionFactories()
|
|
{
|
|
try
|
|
{
|
|
var tenants = new string[_SessionFactories.Keys.Count];
|
|
|
|
_SessionFactories.Keys.CopyTo(tenants, 0);
|
|
|
|
foreach (var item in tenants)
|
|
{
|
|
ResetSessionFactory(item);
|
|
}
|
|
|
|
_SessionFactories = new Dictionary<string, ISessionFactory>();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//ignore?
|
|
}
|
|
}
|
|
|
|
public static void ResetSessionFactory(string tenant)
|
|
{
|
|
var t = tenant;
|
|
if (string.IsNullOrEmpty(t))
|
|
{
|
|
t = CurrentTenant;
|
|
}
|
|
|
|
if (_SessionFactories.ContainsKey(t))
|
|
{
|
|
try
|
|
{
|
|
var fact = _SessionFactories[t];
|
|
if (!fact.IsClosed)
|
|
{
|
|
fact.Close();
|
|
}
|
|
|
|
fact.Dispose();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//ignore exception??
|
|
}
|
|
|
|
_SessionFactories.Remove(t);
|
|
}
|
|
}
|
|
|
|
public static void CloseSession()
|
|
{
|
|
if (currentSession != null)
|
|
{
|
|
currentSession.Close();
|
|
currentSession.Dispose();
|
|
currentSession = null;
|
|
}
|
|
}
|
|
|
|
public static ISession OpenSession()
|
|
{
|
|
try
|
|
{
|
|
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
|
|
|
|
CloseSession();
|
|
var session = SessionFactory.OpenSession(new UpdInsInterceptor()) ?? RebuildSessionFactory().OpenSession(new UpdInsInterceptor());
|
|
|
|
currentSession = session;
|
|
|
|
return currentSession;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new FaultException(e.StackTrace + e.InnerException != null ? e.InnerException.Message : string.Empty);
|
|
}
|
|
}
|
|
|
|
public static bool TenantExists()
|
|
{
|
|
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Multitenancy\" + CurrentTenant + ".config");
|
|
|
|
return File.Exists(path);
|
|
}
|
|
}
|
|
} |