public void ChangeUserSchema(string schema, string dbUser, bool forceChangeSessionFactory = false) { try { if (SFactories.ContainsKey(dbUser) && ((NHibernate.Impl.SessionFactoryImpl)SFactories[dbUser]).Settings.DefaultSchemaName != "\"" + schema + "\"") { Cfg.SetProperty("default_schema", schema); foreach (PersistentClass item in Cfg.ClassMappings) { if (item.Table.Schema != "\"COMMON\"") { item.Table.Schema = "\"" + schema + "\""; ((NHibernate.Mapping.SimpleValue)item.Table.IdentifierValue).IdentifierGeneratorProperties["schema"] = item.Table.Schema; } } CreateSessionFactory(dbUser, forceChangeSessionFactory); AppControllerBase.AppControler.ActivateEntities(); } } catch { throw new iQException("Error en ChangeUserSchema"); } }
public void Close() { foreach (KeyValuePair <string, NHibernate.ISessionFactory> item in SFactories) { item.Value.Close(); item.Value.Dispose(); } SFactories.Clear(); }
public void CloseSessionFactory(string dbUser) { //No cerramos la primera porque se usa para hacer login de usuarios if (SFactories.First().Key == dbUser) { return; } if (SFactories.ContainsKey(dbUser) && SFactories[dbUser] != null) { SFactories[dbUser].Close(); SFactories[dbUser].Dispose(); } }
public void ChangeSessionFactory(string dbUser) { try { if (SFactories.ContainsKey(dbUser)) { RemoveSessionFactory(dbUser, true); CreateSessionFactory(dbUser); } } catch (Exception ex) { RemoveSessionFactory(dbUser); throw ex; } }
public void RemoveSessionFactory(string dbUser, bool overrideFirst = false) { if (SFactories.Count == 0) { return; } //No borramos la primera porque se usa para hacer login de usuarios if (!overrideFirst && SFactories.First().Key == dbUser) { return; } if (SFactories.ContainsKey(dbUser) && SFactories[dbUser] != null) { SFactories[dbUser].Close(); SFactories[dbUser].Dispose(); SFactories.Remove(dbUser); } }
/// <summary> /// Abre una sesión /// </summary> /// <returns>Código de session</returns> /// public int OpenSession() { try { int pos = 0; ISession sess = null; //Usamos el SessionFactory del usuario o del usuario por defecto si no hay usuario logueado if (AppContext.User != null && AppContext.User.IsAuthenticated) { sess = SFactories[Library.User.MapToDBUsername(AppContext.User.Name)].OpenSession(); } else { sess = SFactories.First().Value.OpenSession(); } // Buscamos la primera posicion vacía foreach (ISession session in Sessions) { if (session == null) { Sessions[pos] = sess; return(pos); } pos++; } Sessions.Insert(pos, sess); return(pos); } catch { throw new iQException("SFactories no contiene sesiones activas"); } }
public void CreateSessionFactory(string dbUser, bool forceChangeSessionFactory = false) { try { if (!SFactories.ContainsKey(dbUser)) { try { SFactories.Add(dbUser, _cfg.BuildSessionFactory()); } catch { List <string> servers = new List <string>(); if (SettingsMng.Instance.GetActiveServer() != string.Empty) { servers.Add(SettingsMng.Instance.GetActiveServer()); } if (SettingsMng.Instance.GetLANServer() != string.Empty && !servers.Contains(SettingsMng.Instance.GetLANServer())) { servers.Add(SettingsMng.Instance.GetLANServer()); } if (SettingsMng.Instance.GetWANServer() != string.Empty && !servers.Contains(SettingsMng.Instance.GetWANServer())) { servers.Add(SettingsMng.Instance.GetWANServer()); } servers.Add(nHManager.Instance.Host); for (int i = 0; i <= servers.Count; i++) { string connection = _cfg.GetProperty("hibernate.connection.connection_string"); string new_connection = string.Empty; int pos = 0; pos = connection.IndexOf("Server="); pos += 7; new_connection = connection.Substring(0, pos); connection = connection.Substring(pos); new_connection += servers[i]; pos = connection.IndexOf(";"); new_connection += connection.Substring(pos); _cfg.SetProperty("hibernate.connection.connection_string", new_connection); connection = _cfg.GetProperty("connection.connection_string"); pos = connection.IndexOf("Server="); pos += 7; new_connection = connection.Substring(0, pos); connection = connection.Substring(pos); new_connection += servers[i]; pos = connection.IndexOf(";"); new_connection += connection.Substring(pos); _cfg.SetProperty("connection.connection_string", new_connection); try { SFactories.Add(dbUser, _cfg.BuildSessionFactory()); nHManager.Instance.SetServer(servers[i]); return; } catch { continue; } } } } // Modificamos la SessionFactory solo si se fuerza explicitamente o es aplicacion desktop por si cambia el password, // el host o el dbname. No lo hacemos si es aplicacion web porque si esto ocurre se llama explícitame a ChangeSessionFactory // desde la aplicación, de esta forma se hace una única vez y así mejoramos el rendimiento para cada script else if ((SettingsMng.Instance.GetApplicationType() == EAppType.Desktop) || (forceChangeSessionFactory)) { ChangeSessionFactory(dbUser); } } catch (Exception ex) { RemoveSessionFactory(dbUser); throw ex; } }