How to change default FlushMode to Commit in C#?

北慕城南 提交于 2019-12-31 03:18:14

问题


So... already said it:

How to change FlushMode to Commit in C#?

I mean, In Fluent NHibernate FlushMode by default is setted as Auto.

So... to set FluentMode to Commit, I need to open session and then change It:

var someSessionFactory = ... bla bla ..;
var session = someSessionFactory.OpenSession();
session.FlushMode = FlushMode.Commit;

This will work but... this will mean that I need to call method which contains FlushMode.Commit each time I am opening session. To inicialize sessionFactory I have several wraps (meant to set it only once and then auto use it when new context is opened), which means I can't just open session directly every time I want without digging into factory type and etc.

Is there a way to change default FlushMode from Auto to Commit? Is there a way to do it in var sessionFactory = Fluently.Configure(). ... ?

EDIT:

Tried seccond thing

public void Initialise(params Assembly[] mappingAssemblies)
{
    this._sessionFactory = Fluently.Configure()
        .Database(
            MsSqlConfiguration.MsSql2008
                .ConnectionString(this._connectionString)
                .AdoNetBatchSize(10)
                .QuerySubstitutions("true 1, false 0, yes 'Y', no 'N'"))
        .Cache(c => c.Not.UseSecondLevelCache().Not.UseQueryCache())
        .Mappings(m =>
        {
            foreach (Assembly asm in mappingAssemblies)
            {
                m.FluentMappings.AddFromAssembly(asm);
                m.HbmMappings.AddFromAssembly(asm);
            }
        })
        .ExposeConfiguration(ModifyConfiguration)
        .BuildSessionFactory();

    var session = _sessionFactory.OpenSession();
}

public void ModifyConfiguration(NHibernate.Cfg.Configuration configuration)
{
    configuration.Properties["default_flush_mode"] = FlushMode.Commit.ToString();
}

I called _sessionFActory.OpenSession() to see if FlushMode has changed and... Nope. Still FlushMode is Auto, instead of Commit.


回答1:


There is no way how to configure default FlushMode for ISession. The way I do that, and the way which could be found in common, is step into ISessionFactory.OpenSession() (IoC, MVC AOP Filter, Web API delegate) and assign the FlushMode (manually)

var session = SessionFactory.OpenSession();
session.FlushMode = FlushMode.Commit;

Check these:

  • Yet another way to manage your NHibernate ISessionFactory
  • NHibernate and the Unit of Work Pattern

The property ISession.FlushMode as defined below:

public interface ISession : IDisposable
{
    ...
    /// <summary>
    /// Determines at which points Hibernate automatically flushes the session.
    /// 
    /// </summary>
    /// 
    /// <remarks>
    /// For a readonly session, it is reasonable to set the flush mode 
    ///  to <c>FlushMode.Never</c>
    ///  at the start of the session (in order to achieve some 
    ///       extra performance).
    /// 
    /// </remarks>
    FlushMode FlushMode { get; set; }

and it's the default implementation snippet:

public sealed class SessionImpl : ...
{
    ...
    private FlushMode flushMode = FlushMode.Auto;
    ...

is not set anyhow during the ISessionFactory.OpenSession() call.

ORIGINAL, not working approach

The documented <hibernate-configuration> setting default_flush_mode is not supported.

So, we have these configuration properties available for <hibernate-configuration> (default / not fluent configuration settings):

3.5. Optional configuration properties

  • default_flush_mode - The default FlushMode, defaults to Unspecified eg.
    • Unspecified | Never | Commit | Auto | Always

and based e.g. on this Q & A:

NHibernate config properties in Fluent NHibernate

we can do:

Fluently.Configure()
    .Database(ConfigureDatabase())
    .Mappings(ConfigureMapping)
    .ExposeConfiguration(ModifyConfiguration)
    .BuildConfiguration();

...

private void ModifyConfiguration(Configuration configuration)
{
    configuration.Properties["default_flush_mode"] = "Commit";
}



回答2:


Apparently this feature will be available in version 4.1.0, and recently added in this checkin,

according to the documentation it's configured as follows

var cfg = new Configuration().Configure();
cfg.SessionFactory().DefaultFlushMode(FlushMode.Always);


来源:https://stackoverflow.com/questions/30724958/how-to-change-default-flushmode-to-commit-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!