2

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.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Olegs Jasjko
  • 2,128
  • 6
  • 27
  • 47

2 Answers2

5

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:

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";
}
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Will try it. Do i need to send some values into configutarion, or it will understand it authomatically? – Olegs Jasjko Jun 09 '15 at 07:05
  • The `Configuration` is a native, built-in object in NHiberante. There are some built in ways how to populate it from the .config file *(the section ``)*. Fluent NHiberante, in fact, does almost the same, just in code. At the end, it does pass that object to our method ... already configured with fluent defaults. We are just about to extend it... hope it helps ;) – Radim Köhler Jun 09 '15 at 07:08
  • Hm... didn't worked so far. Maybe something wrong with my wraps because I actually failing to detect, is `Fluent.Configure()` changes are affected somehow to SessionFactory... is there a way to see how configuration affected SessionFactory or where it is stored? (The only thing I managed to find inside SessionFactory are mapped classes). – Olegs Jasjko Jun 09 '15 at 07:31
  • If someone is still here. I updated my question and showed the whole `Fluemt.Configure()`. Maybe something there are preventing FlushMode from change? – Olegs Jasjko Jun 09 '15 at 08:24
  • @OlegsJasjko, I was wrong... when expecting that the cited doc part is correct. I am using the explicit `session = SessionFactory.OpenSession(); session.FlushMode = FlushMode.Commit;` ... and observed the source code ... to find out that for a reason. This is the only way how to apply the different setting then the default one – Radim Köhler Jun 09 '15 at 09:49
  • Eh... a sad thing indeed... well, anyway, thanks for attempt to help me :) – Olegs Jasjko Jun 09 '15 at 10:01
  • Could be better I know ;) Anyhow, this way I am using it for ages... just forgot ;) Enjoy NHibernate, Olegs. – Radim Köhler Jun 09 '15 at 10:02
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);
Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43