From 95dda1db6446e40d31b2a79bfee77bc6dcba1949 Mon Sep 17 00:00:00 2001 From: maca88 Date: Fri, 28 Dec 2018 16:23:26 +0100 Subject: [PATCH 1/2] Cache Dialect when creating a session factory Back-ported from 5.3 (cherry picked from commit cbc25278311280b10a3787b9e2cc7209cb9a782f) --- src/NHibernate/Cfg/Configuration.cs | 36 ++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/NHibernate/Cfg/Configuration.cs b/src/NHibernate/Cfg/Configuration.cs index 6b70fa340a2..0ff46e9f41b 100644 --- a/src/NHibernate/Cfg/Configuration.cs +++ b/src/NHibernate/Cfg/Configuration.cs @@ -239,6 +239,40 @@ public bool HasNonIdentifierPropertyNamedId(string className) NHibernate.Dialect.Dialect.GetDialect(configuration.Properties); } + [Serializable] + private class StaticDialectMappingWrapper : IMapping + { + private readonly IMapping _mapping; + + public StaticDialectMappingWrapper(IMapping mapping) + { + _mapping = mapping; + Dialect = mapping.Dialect; + } + + public IType GetIdentifierType(string className) + { + return _mapping.GetIdentifierType(className); + } + + public string GetIdentifierPropertyName(string className) + { + return _mapping.GetIdentifierPropertyName(className); + } + + public IType GetReferencedPropertyType(string className, string propertyName) + { + return _mapping.GetReferencedPropertyType(className, propertyName); + } + + public bool HasNonIdentifierPropertyNamedId(string className) + { + return _mapping.HasNonIdentifierPropertyNamedId(className); + } + + public Dialect.Dialect Dialect { get; } + } + private IMapping mapping; protected Configuration(SettingsFactory settingsFactory) @@ -1272,7 +1306,7 @@ public ISessionFactory BuildSessionFactory() // Ok, don't need schemas anymore, so free them Schemas = null; - return new SessionFactoryImpl(this, mapping, settings, GetInitializedEventListeners()); + return new SessionFactoryImpl(this, new StaticDialectMappingWrapper(mapping), settings, GetInitializedEventListeners()); } /// From 8bc24b7b9bb6c694b568bb3d25cd9cf6d1db7198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= <12201973+fredericdelaporte@users.noreply.github.com> Date: Sun, 17 Feb 2019 13:46:42 +0100 Subject: [PATCH 2/2] Use a statically resolved dialect when building the session factory This by the way allows NHibernate.Spatial dialect hack to work again, see nhibernate/NHibernate.Spatial#104 --- src/NHibernate/Cfg/Configuration.cs | 35 +++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/NHibernate/Cfg/Configuration.cs b/src/NHibernate/Cfg/Configuration.cs index 0ff46e9f41b..7785974ff91 100644 --- a/src/NHibernate/Cfg/Configuration.cs +++ b/src/NHibernate/Cfg/Configuration.cs @@ -1288,6 +1288,7 @@ protected virtual void ConfigureProxyFactoryFactory() #endregion } + /// /// Instantiate a new , using the properties and mappings in this /// configuration. The will be immutable, so changes made to the @@ -1296,17 +1297,33 @@ protected virtual void ConfigureProxyFactoryFactory() /// An instance. public ISessionFactory BuildSessionFactory() { + var dynamicDialectMapping = mapping; + // Use a mapping which does store the dialect instead of instantiating a new one + // at each access. The dialect does not change while building a session factory. + // It furthermore allows some hack on NHibernate.Spatial side to go on working, + // See nhibernate/NHibernate.Spatial#104 + mapping = new StaticDialectMappingWrapper(mapping); + try + { + ConfigureProxyFactoryFactory(); + SecondPassCompile(); + Validate(); + Environment.VerifyProperties(properties); + Settings settings = BuildSettings(); - ConfigureProxyFactoryFactory(); - SecondPassCompile(); - Validate(); - Environment.VerifyProperties(properties); - Settings settings = BuildSettings(); - - // Ok, don't need schemas anymore, so free them - Schemas = null; + // Ok, don't need schemas anymore, so free them + Schemas = null; - return new SessionFactoryImpl(this, new StaticDialectMappingWrapper(mapping), settings, GetInitializedEventListeners()); + return new SessionFactoryImpl( + this, + mapping, + settings, + GetInitializedEventListeners()); + } + finally + { + mapping = dynamicDialectMapping; + } } ///