Skip to content

Commit c2de761

Browse files
rjpereshazzik
authored andcommitted
NH-3619 - Make default value of FlushMode configurable
1 parent 490dcb9 commit c2de761

11 files changed

+110
-3
lines changed

src/NHibernate.Test/App.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
<property name="format_sql">true</property>
5252
<property name="show_sql">false</property>
5353
<property name="command_timeout">444</property>
54+
55+
<property name="default_flush_mode">Commit</property>
5456

5557
</session-factory>
5658
</hibernate-configuration>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Text;
4+
using NHibernate.Cfg;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.CfgTest
8+
{
9+
[TestFixture]
10+
public class DefaultFlushModeFixture
11+
{
12+
[Test]
13+
public void CanSetDefaultFlushModeThroughXmlConfiguration()
14+
{
15+
var cfg = new Configuration().Configure();
16+
17+
using (var sessionFactory = cfg.BuildSessionFactory())
18+
{
19+
using (var session = sessionFactory.OpenSession())
20+
{
21+
Assert.AreEqual(session.FlushMode, FlushMode.Commit);
22+
}
23+
}
24+
}
25+
26+
[Test]
27+
public void CanSetDefaultFlushModeThroughStandardConfiguration()
28+
{
29+
var cfg = new Configuration().Configure();
30+
cfg.Properties[Environment.DefaultFlushMode] = FlushMode.Always.ToString();
31+
32+
using (var sessionFactory = cfg.BuildSessionFactory())
33+
{
34+
using (var session = sessionFactory.OpenSession())
35+
{
36+
Assert.AreEqual(session.FlushMode, FlushMode.Always);
37+
}
38+
}
39+
40+
cfg.Properties[Environment.DefaultFlushMode] = FlushMode.Commit.ToString();
41+
42+
using (var sessionFactory = cfg.BuildSessionFactory())
43+
{
44+
using (var session = sessionFactory.OpenSession())
45+
{
46+
Assert.AreEqual(session.FlushMode, FlushMode.Commit);
47+
}
48+
}
49+
}
50+
51+
[Test]
52+
public void CanSetDefaultFlushModeThroughLoquaciousConfiguration()
53+
{
54+
var cfg = new Configuration()
55+
.Configure();
56+
57+
cfg
58+
.SessionFactory()
59+
.DefaultFlushMode(FlushMode.Always);
60+
61+
using (var sessionFactory = cfg.BuildSessionFactory())
62+
{
63+
using (var session = sessionFactory.OpenSession())
64+
{
65+
Assert.AreEqual(session.FlushMode, FlushMode.Always);
66+
}
67+
}
68+
69+
cfg.Configure()
70+
.SessionFactory()
71+
.DefaultFlushMode(FlushMode.Commit);
72+
73+
using (var sessionFactory = cfg.BuildSessionFactory())
74+
{
75+
using (var session = sessionFactory.OpenSession())
76+
{
77+
Assert.AreEqual(session.FlushMode, FlushMode.Commit);
78+
}
79+
}
80+
}
81+
}
82+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
<Compile Include="CfgTest\ConfigurationSchemaFixture.cs" />
167167
<Compile Include="CfgTest\ConfigurationSerializationTests.cs" />
168168
<Compile Include="CfgTest\CustomBytecodeProviderTest.cs" />
169+
<Compile Include="CfgTest\DefaultFlushModeFixture.cs" />
169170
<Compile Include="CfgTest\DefaultNsAssmFixture.cs" />
170171
<Compile Include="CfgTest\EntityCacheUsageParserFixture.cs" />
171172
<Compile Include="CfgTest\HbmOrderingFixture.cs" />

src/NHibernate/Cfg/Environment.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ public static string Version
9090
/// <summary>The EntityMode in which set the Session opened from the SessionFactory.</summary>
9191
public const string DefaultEntityMode = "default_entity_mode";
9292

93+
/// Implementation of NH-3619 - Make default value of FlushMode configurable
94+
public const string DefaultFlushMode = "default_flush_mode";
95+
9396
/// <summary>
9497
/// When using an enhanced id generator and pooled optimizers (<see cref="NHibernate.Id.Enhanced.IOptimizer"/>),
9598
/// prefer interpreting the database value as the lower (lo) boundary. The default is to interpret it as the high boundary.

src/NHibernate/Cfg/Loquacious/FluentSessionFactoryConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public IFluentSessionFactoryConfiguration Using(EntityMode entityMode)
4747
return this;
4848
}
4949

50+
public IFluentSessionFactoryConfiguration DefaultFlushMode(FlushMode flushMode)
51+
{
52+
configuration.SetProperty(Environment.DefaultFlushMode, flushMode.ToString());
53+
return this;
54+
}
55+
5056
public IFluentSessionFactoryConfiguration ParsingHqlThrough<TQueryTranslator>()
5157
where TQueryTranslator : IQueryTranslatorFactory
5258
{

src/NHibernate/Cfg/Loquacious/IFluentSessionFactoryConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public interface IFluentSessionFactoryConfiguration
2626

2727
IFluentSessionFactoryConfiguration GenerateStatistics();
2828
IFluentSessionFactoryConfiguration Using(EntityMode entityMode);
29+
IFluentSessionFactoryConfiguration DefaultFlushMode(FlushMode flushMode);
30+
2931
IFluentSessionFactoryConfiguration ParsingHqlThrough<TQueryTranslator>() where TQueryTranslator : IQueryTranslatorFactory;
3032
IFluentSessionFactoryConfiguration ParsingLinqThrough<TQueryProvider>() where TQueryProvider : INhQueryProvider;
3133

src/NHibernate/Cfg/Settings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ public Settings()
110110

111111
public bool IsOrderInsertsEnabled { get; internal set; }
112112

113+
public FlushMode DefaultFlushMode { get; internal set; }
114+
113115
public EntityMode DefaultEntityMode { get; internal set; }
114116

115117
public bool IsDataDefinitionImplicitCommit { get; internal set; }

src/NHibernate/Cfg/SettingsFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ public Settings BuildSettings(IDictionary<string, string> properties)
259259
}
260260
}
261261

262+
//NH-3619
263+
FlushMode defaultFlushMode = (FlushMode) Enum.Parse(typeof(FlushMode), PropertiesHelper.GetString(Environment.DefaultFlushMode, properties, FlushMode.Unspecified.ToString()), false);
264+
log.Info("Default flush mode: " + defaultFlushMode);
265+
settings.DefaultFlushMode = defaultFlushMode;
266+
262267
EntityMode defaultEntityMode =
263268
EntityModeHelper.Parse(PropertiesHelper.GetString(Environment.DefaultEntityMode, properties, "poco"));
264269
log.Info("Default entity-mode: " + defaultEntityMode);

src/NHibernate/Impl/SessionFactoryImpl.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ public ISession OpenSession(IDbConnection connection, bool flushBeforeCompletion
497497
return
498498
new SessionImpl(connection, this, true, settings.CacheProvider.NextTimestamp(), interceptor,
499499
settings.DefaultEntityMode, flushBeforeCompletionEnabled, autoCloseSessionEnabled,
500-
isInterceptorsBeforeTransactionCompletionIgnoreExceptionsEnabled, connectionReleaseMode);
500+
isInterceptorsBeforeTransactionCompletionIgnoreExceptionsEnabled, connectionReleaseMode, settings.DefaultFlushMode);
501501
}
502502

503503
public IEntityPersister GetEntityPersister(string entityName)
@@ -1215,7 +1215,8 @@ private SessionImpl OpenSession(IDbConnection connection, bool autoClose, long t
12151215
SessionImpl session = new SessionImpl(connection, this, autoClose, timestamp, sessionLocalInterceptor ?? interceptor,
12161216
settings.DefaultEntityMode, settings.IsFlushBeforeCompletionEnabled,
12171217
settings.IsAutoCloseSessionEnabled, isInterceptorsBeforeTransactionCompletionIgnoreExceptionsEnabled,
1218-
settings.ConnectionReleaseMode);
1218+
settings.ConnectionReleaseMode, settings.DefaultFlushMode);
1219+
12191220
if (sessionLocalInterceptor != null)
12201221
{
12211222
// NH specific feature

src/NHibernate/Impl/SessionImpl.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ internal SessionImpl(
215215
bool flushBeforeCompletionEnabled,
216216
bool autoCloseSessionEnabled,
217217
bool ignoreExceptionBeforeTransactionCompletion,
218-
ConnectionReleaseMode connectionReleaseMode)
218+
ConnectionReleaseMode connectionReleaseMode,
219+
FlushMode defaultFlushMode)
219220
: base(factory)
220221
{
221222
using (new SessionIdLoggingContext(SessionId))
@@ -235,6 +236,7 @@ internal SessionImpl(
235236
this.connectionReleaseMode = connectionReleaseMode;
236237
this.ignoreExceptionBeforeTransactionCompletion = ignoreExceptionBeforeTransactionCompletion;
237238
connectionManager = new ConnectionManager(this, connection, connectionReleaseMode, interceptor);
239+
this.flushMode = defaultFlushMode;
238240

239241
if (factory.Statistics.IsStatisticsEnabled)
240242
{

0 commit comments

Comments
 (0)