Skip to content

Use a statically resolved dialect when building the session factory #2011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 60 additions & 9 deletions src/NHibernate/Cfg/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1254,6 +1288,7 @@ protected virtual void ConfigureProxyFactoryFactory()

#endregion
}

/// <summary>
/// Instantiate a new <see cref="ISessionFactory" />, using the properties and mappings in this
/// configuration. The <see cref="ISessionFactory" /> will be immutable, so changes made to the
Expand All @@ -1262,17 +1297,33 @@ protected virtual void ConfigureProxyFactoryFactory()
/// <returns>An <see cref="ISessionFactory" /> instance.</returns>
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, mapping, settings, GetInitializedEventListeners());
return new SessionFactoryImpl(
this,
mapping,
settings,
GetInitializedEventListeners());
}
finally
{
mapping = dynamicDialectMapping;
}
}

/// <summary>
Expand Down