Skip to content

NH-3619 - Make default value of FlushMode configurable #315

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 1 commit into from
Feb 22, 2015
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/NHibernate.Test/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
<property name="format_sql">true</property>
<property name="show_sql">false</property>
<property name="command_timeout">444</property>

<property name="default_flush_mode">Commit</property>

</session-factory>
</hibernate-configuration>
Expand Down
82 changes: 82 additions & 0 deletions src/NHibernate.Test/CfgTest/DefaultFlushModeFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NUnit.Framework;

namespace NHibernate.Test.CfgTest
{
[TestFixture]
public class DefaultFlushModeFixture
{
[Test]
public void CanSetDefaultFlushModeThroughXmlConfiguration()
{
var cfg = new Configuration().Configure();

using (var sessionFactory = cfg.BuildSessionFactory())
{
using (var session = sessionFactory.OpenSession())
{
Assert.AreEqual(session.FlushMode, FlushMode.Commit);
}
}
}

[Test]
public void CanSetDefaultFlushModeThroughStandardConfiguration()
{
var cfg = new Configuration().Configure();
cfg.Properties[Environment.DefaultFlushMode] = FlushMode.Always.ToString();

using (var sessionFactory = cfg.BuildSessionFactory())
{
using (var session = sessionFactory.OpenSession())
{
Assert.AreEqual(session.FlushMode, FlushMode.Always);
}
}

cfg.Properties[Environment.DefaultFlushMode] = FlushMode.Commit.ToString();

using (var sessionFactory = cfg.BuildSessionFactory())
{
using (var session = sessionFactory.OpenSession())
{
Assert.AreEqual(session.FlushMode, FlushMode.Commit);
}
}
}

[Test]
public void CanSetDefaultFlushModeThroughLoquaciousConfiguration()
{
var cfg = new Configuration()
.Configure();

cfg
.SessionFactory()
.DefaultFlushMode(FlushMode.Always);

using (var sessionFactory = cfg.BuildSessionFactory())
{
using (var session = sessionFactory.OpenSession())
{
Assert.AreEqual(session.FlushMode, FlushMode.Always);
}
}

cfg.Configure()
.SessionFactory()
.DefaultFlushMode(FlushMode.Commit);

using (var sessionFactory = cfg.BuildSessionFactory())
{
using (var session = sessionFactory.OpenSession())
{
Assert.AreEqual(session.FlushMode, FlushMode.Commit);
}
}
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
<Compile Include="CfgTest\ConfigurationSchemaFixture.cs" />
<Compile Include="CfgTest\ConfigurationSerializationTests.cs" />
<Compile Include="CfgTest\CustomBytecodeProviderTest.cs" />
<Compile Include="CfgTest\DefaultFlushModeFixture.cs" />
<Compile Include="CfgTest\DefaultNsAssmFixture.cs" />
<Compile Include="CfgTest\EntityCacheUsageParserFixture.cs" />
<Compile Include="CfgTest\HbmOrderingFixture.cs" />
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate/Cfg/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public static string Version
/// <summary>The EntityMode in which set the Session opened from the SessionFactory.</summary>
public const string DefaultEntityMode = "default_entity_mode";

/// Implementation of NH-3619 - Make default value of FlushMode configurable
public const string DefaultFlushMode = "default_flush_mode";

/// <summary>
/// When using an enhanced id generator and pooled optimizers (<see cref="NHibernate.Id.Enhanced.IOptimizer"/>),
/// prefer interpreting the database value as the lower (lo) boundary. The default is to interpret it as the high boundary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public IFluentSessionFactoryConfiguration Using(EntityMode entityMode)
return this;
}

public IFluentSessionFactoryConfiguration DefaultFlushMode(FlushMode flushMode)
{
configuration.SetProperty(Environment.DefaultFlushMode, flushMode.ToString());
return this;
}

public IFluentSessionFactoryConfiguration ParsingHqlThrough<TQueryTranslator>()
where TQueryTranslator : IQueryTranslatorFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface IFluentSessionFactoryConfiguration

IFluentSessionFactoryConfiguration GenerateStatistics();
IFluentSessionFactoryConfiguration Using(EntityMode entityMode);
IFluentSessionFactoryConfiguration DefaultFlushMode(FlushMode flushMode);

IFluentSessionFactoryConfiguration ParsingHqlThrough<TQueryTranslator>() where TQueryTranslator : IQueryTranslatorFactory;
IFluentSessionFactoryConfiguration ParsingLinqThrough<TQueryProvider>() where TQueryProvider : INhQueryProvider;

Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/Cfg/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public Settings()

public bool IsOrderInsertsEnabled { get; internal set; }

public FlushMode DefaultFlushMode { get; internal set; }

public EntityMode DefaultEntityMode { get; internal set; }

public bool IsDataDefinitionImplicitCommit { get; internal set; }
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/Cfg/SettingsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ public Settings BuildSettings(IDictionary<string, string> properties)
}
}

//NH-3619
FlushMode defaultFlushMode = (FlushMode) Enum.Parse(typeof(FlushMode), PropertiesHelper.GetString(Environment.DefaultFlushMode, properties, FlushMode.Unspecified.ToString()), false);
log.Info("Default flush mode: " + defaultFlushMode);
settings.DefaultFlushMode = defaultFlushMode;

EntityMode defaultEntityMode =
EntityModeHelper.Parse(PropertiesHelper.GetString(Environment.DefaultEntityMode, properties, "poco"));
log.Info("Default entity-mode: " + defaultEntityMode);
Expand Down
5 changes: 3 additions & 2 deletions src/NHibernate/Impl/SessionFactoryImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public ISession OpenSession(IDbConnection connection, bool flushBeforeCompletion
return
new SessionImpl(connection, this, true, settings.CacheProvider.NextTimestamp(), interceptor,
settings.DefaultEntityMode, flushBeforeCompletionEnabled, autoCloseSessionEnabled,
isInterceptorsBeforeTransactionCompletionIgnoreExceptionsEnabled, connectionReleaseMode);
isInterceptorsBeforeTransactionCompletionIgnoreExceptionsEnabled, connectionReleaseMode, settings.DefaultFlushMode);
}

public IEntityPersister GetEntityPersister(string entityName)
Expand Down Expand Up @@ -1215,7 +1215,8 @@ private SessionImpl OpenSession(IDbConnection connection, bool autoClose, long t
SessionImpl session = new SessionImpl(connection, this, autoClose, timestamp, sessionLocalInterceptor ?? interceptor,
settings.DefaultEntityMode, settings.IsFlushBeforeCompletionEnabled,
settings.IsAutoCloseSessionEnabled, isInterceptorsBeforeTransactionCompletionIgnoreExceptionsEnabled,
settings.ConnectionReleaseMode);
settings.ConnectionReleaseMode, settings.DefaultFlushMode);

if (sessionLocalInterceptor != null)
{
// NH specific feature
Expand Down
4 changes: 3 additions & 1 deletion src/NHibernate/Impl/SessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ internal SessionImpl(
bool flushBeforeCompletionEnabled,
bool autoCloseSessionEnabled,
bool ignoreExceptionBeforeTransactionCompletion,
ConnectionReleaseMode connectionReleaseMode)
ConnectionReleaseMode connectionReleaseMode,
FlushMode defaultFlushMode)
: base(factory)
{
using (new SessionIdLoggingContext(SessionId))
Expand All @@ -235,6 +236,7 @@ internal SessionImpl(
this.connectionReleaseMode = connectionReleaseMode;
this.ignoreExceptionBeforeTransactionCompletion = ignoreExceptionBeforeTransactionCompletion;
connectionManager = new ConnectionManager(this, connection, connectionReleaseMode, interceptor);
this.flushMode = defaultFlushMode;

if (factory.Statistics.IsStatisticsEnabled)
{
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/nhibernate-configuration.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<xs:enumeration value="adonet.factory_class" />
<xs:enumeration value="default_batch_fetch_size" />
<xs:enumeration value="default_entity_mode" />
<xs:enumeration value="default_flush_mode" />
<xs:enumeration value="use_sql_comments" />
<xs:enumeration value="format_sql" />
<xs:enumeration value="collectiontype.factory_class" />
Expand Down