public ISession GetSessionFrom(string sessionFactoryName, bool applyDefaultFilters = true) { ISession session = (ISession)ContextSessions[sessionFactoryName]; if (session == null) { OpenSessionInViewSection openSessionInViewSection = (OpenSessionInViewSection)ConfigurationManager.GetSection("nhibernateSettings"); if (!string.IsNullOrEmpty(openSessionInViewSection.SessionFactories.Interceptor) && openSessionInViewSection.SessionFactories.DefaultSessionFactory == sessionFactoryName) { var interceptor = (IInterceptor)Activator.CreateInstance(Type.GetType(openSessionInViewSection.SessionFactories.Interceptor)); session = GetSessionFactoryFor(sessionFactoryName).OpenSession(interceptor); //session = GetSessionFactoryFor(sessionFactoryName).OpenSession(new SqlStatementInterceptor()); } else { session = GetSessionFactoryFor(sessionFactoryName).OpenSession(); } ContextSessions[sessionFactoryName] = session; } if (NHibernateSessionUtil.ApplyFilterActions.Count > 0 && applyDefaultFilters) { foreach (Action <ISession> applyFilterAction in NHibernateSessionUtil.ApplyFilterActions) { applyFilterAction(session); } } if (session == null) { throw new Exception("session was null"); } return(session); }
public void CommitAndCloseSession(object sender, EventArgs e) { OpenSessionInViewSection openSessionInViewSection = GetOpenSessionInViewSection(); try { foreach (SessionFactoryElement sessionFactorySettings in openSessionInViewSection.SessionFactories) { if (sessionFactorySettings.IsTransactional) { NHibernateSessionManager.Instance.CommitTransactionOn(sessionFactorySettings.Name); } } } finally { foreach (SessionFactoryElement sessionFactorySettings in openSessionInViewSection.SessionFactories) { NHibernateSessionManager.Instance.CloseSessionOn(sessionFactorySettings.Name); } } }
private ISessionFactory GetSessionFactoryFor(string sessionFactoryName) { if (string.IsNullOrEmpty(sessionFactoryName)) { throw new ArgumentNullException("sessionFactoryName", "sessionFactoryName may not be null nor empty"); } ISessionFactory sessionFactory = (ISessionFactory)sessionFactories[sessionFactoryName]; if (sessionFactory != null) { return(sessionFactory); } //Uso esclusivo delle risorse - Concorrenza lock (sessionFactories) { sessionFactory = (ISessionFactory)sessionFactories[sessionFactoryName]; if (sessionFactory == null) { OpenSessionInViewSection openSessionInViewSection = (OpenSessionInViewSection)ConfigurationManager.GetSection("nhibernateSettings"); if (openSessionInViewSection == null) { throw new Exception("Impossibile trovare la sezione nhibernateSettings nel ConfigurationManager."); } SessionFactoryElement factoryElement = openSessionInViewSection.SessionFactories[sessionFactoryName]; if (factoryElement == null) { throw new Exception("The SessionFactory with name " + sessionFactoryName + " Element was not found with ConfigurationManager."); } INHibernateConfiguration configurator; try { configurator = (INHibernateConfiguration)Activator.CreateInstance(Type.GetType(factoryElement.Configurator)); } catch (Exception ex) { throw new Exception("Errore in creazione Configurator. Verificare riferimento a VecompSoftware.DocSuiteWeb.Data.dll", ex); } Configuration configuration = configurator.GetConfigurationFor(factoryElement); #if DEBUG configuration.SetProperty("generate_statistics", "true"); #endif sessionFactory = configuration.BuildSessionFactory(); if (sessionFactory == null) { throw new InvalidOperationException("configuration.BuildSessionFactory() returned null."); } sessionFactories.Add(sessionFactoryName, sessionFactory); sessionConfigurations.Add(sessionFactoryName, configuration); } } return(sessionFactory); }