Skip to content

Commit 15c0eda

Browse files
committed
NH-3771 - Implement BatchVersionedData factory setting
1 parent e3c4267 commit 15c0eda

File tree

9 files changed

+121
-14
lines changed

9 files changed

+121
-14
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System.Collections;
2+
using NHibernate.AdoNet;
3+
using NHibernate.Cfg;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH3771
7+
{
8+
[TestFixture]
9+
public class Fixture : BugTestCase
10+
{
11+
protected override void Configure(Configuration configuration)
12+
{
13+
configuration.SetProperty(Environment.BatchVersionedData, "true");
14+
configuration.SetProperty(Environment.FormatSql, "false");
15+
configuration.SetProperty(Environment.GenerateStatistics, "true");
16+
configuration.SetProperty(Environment.BatchSize, "10");
17+
}
18+
19+
protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)
20+
{
21+
return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory);
22+
}
23+
24+
[Test]
25+
[Description("Should be two batchs with two sentences each.")]
26+
public void InsertAndUpdateWithBatch()
27+
{
28+
sessions.Statistics.Clear();
29+
30+
using (var sqlLog = new SqlLogSpy())
31+
using (ISession s = sessions.OpenSession())
32+
using (ITransaction tx = s.BeginTransaction())
33+
{
34+
Singer vs1 = new Singer();
35+
vs1.Id = 1;
36+
vs1.Name = "Fabrizio De Andre";
37+
s.Save(vs1);
38+
39+
Singer vs2 = new Singer();
40+
vs2.Id = 2;
41+
vs2.Name = "Vinicio Capossela";
42+
s.Save(vs2);
43+
44+
s.Flush();
45+
46+
vs1.Name = "De Andre, Fabrizio";
47+
vs2.Name = "Capossela, Vinicio";
48+
49+
s.Flush();
50+
51+
string log = sqlLog.GetWholeLog();
52+
53+
string[] separator = { System.Environment.NewLine };
54+
string[] lines = log.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);
55+
56+
int batchs = 0;
57+
int sqls = 0;
58+
int batchCommands = 0;
59+
foreach (string line in lines)
60+
{
61+
if (line.StartsWith("NHibernate.SQL") && !line.StartsWith("NHibernate.SQL Batch commands:"))
62+
sqls++;
63+
64+
if (line.StartsWith("NHibernate.SQL Batch commands:"))
65+
batchs++;
66+
67+
if (line.StartsWith("command"))
68+
batchCommands++;
69+
}
70+
71+
Assert.AreEqual(2, batchs);
72+
Assert.AreEqual(0, sqls);
73+
Assert.AreEqual(4, batchCommands);
74+
Assert.AreEqual(2, sessions.Statistics.PrepareStatementCount);
75+
76+
tx.Rollback();
77+
}
78+
}
79+
}
80+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping
3+
xmlns="urn:nhibernate-mapping-2.2"
4+
assembly="NHibernate.Test"
5+
namespace="NHibernate.Test.NHSpecificTest.NH3771">
6+
7+
<class name="Singer">
8+
<id name="Id"/>
9+
<version name="Version"/>
10+
<property name="Name" type="String" />
11+
</class>
12+
13+
14+
</hibernate-mapping>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3771
5+
{
6+
public class Singer
7+
{
8+
public virtual long Id { get; set; }
9+
public virtual string Name { get; set; }
10+
public virtual int Version { get; set; }
11+
}
12+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,8 @@
959959
<Compile Include="NHSpecificTest\NH3634\PersonMapper.cs" />
960960
<Compile Include="NHSpecificTest\NH3727\Entity.cs" />
961961
<Compile Include="NHSpecificTest\NH3727\FixtureByCode.cs" />
962+
<Compile Include="NHSpecificTest\NH3771\Fixture.cs" />
963+
<Compile Include="NHSpecificTest\NH3771\Model.cs" />
962964
<Compile Include="NHSpecificTest\NH3795\Fixture.cs" />
963965
<Compile Include="NHSpecificTest\NH3844\Domain.cs" />
964966
<Compile Include="NHSpecificTest\NH3844\Fixture.cs" />
@@ -3269,6 +3271,7 @@
32693271
<EmbeddedResource Include="NHSpecificTest\NH2204\Mappings.hbm.xml" />
32703272
<EmbeddedResource Include="NHSpecificTest\NH3874\Mappings.hbm.xml" />
32713273
<EmbeddedResource Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\Mappings.hbm.xml" />
3274+
<EmbeddedResource Include="NHSpecificTest\NH3771\Mappings.hbm.xml" />
32723275
<EmbeddedResource Include="NHSpecificTest\NH2218\Mappings.hbm.xml" />
32733276
<EmbeddedResource Include="NHSpecificTest\NH3046\Mappings.hbm.xml" />
32743277
<EmbeddedResource Include="NHSpecificTest\NH3518\Mappings.hbm.xml" />

src/NHibernate/Cfg/Environment.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ public static string Version
113113
// Unused, not implemented
114114
public const string StatementFetchSize = "jdbc.fetch_size";
115115

116-
public const string BatchVersionedData = "jdbc.batch_versioned_data";
117-
118116
// Unused, not implemented
119117
public const string OutputStylesheet = "xml.output_stylesheet";
120118

@@ -154,6 +152,7 @@ public static string Version
154152
// Unused, not implemented
155153
public const string SqlExceptionConverter = "sql_exception_converter";
156154

155+
public const string BatchVersionedData = "adonet.batch_versioned_data";
157156
public const string WrapResultSets = "adonet.wrap_result_sets";
158157
public const string BatchSize = "adonet.batch_size";
159158
public const string BatchStrategy = "adonet.factory_class";

src/NHibernate/Cfg/Settings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public Settings()
2929
#region JDBC Specific (Not Ported)
3030

3131
//private int jdbcFetchSize;
32-
//private bool isJdbcBatchVersionedData;
3332

3433
#endregion
3534
public SqlStatementLogger SqlStatementLogger { get; internal set; }
@@ -118,6 +117,8 @@ public Settings()
118117

119118
public bool IsNamedQueryStartupCheckingEnabled { get; internal set; }
120119

120+
public bool IsBatchVersionedDataEnabled { get; internal set; }
121+
121122
#region NH specific
122123

123124
public IsolationLevel IsolationLevel { get; internal set; }

src/NHibernate/Cfg/SettingsFactory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ public Settings BuildSettings(IDictionary<string, string> properties)
230230

231231
//ADO.NET and connection settings:
232232

233-
// TODO: Environment.BatchVersionedData
234233
settings.AdoBatchSize = PropertiesHelper.GetInt32(Environment.BatchSize, properties, 0);
235234
bool orderInserts = PropertiesHelper.GetBoolean(Environment.OrderInserts, properties, (settings.AdoBatchSize > 0));
236235
log.Info("Order SQL inserts for batching: " + EnabledDisabled(orderInserts));
@@ -243,6 +242,11 @@ public Settings BuildSettings(IDictionary<string, string> properties)
243242
bool wrapResultSets = PropertiesHelper.GetBoolean(Environment.WrapResultSets, properties, false);
244243
log.Debug("Wrap result sets: " + EnabledDisabled(wrapResultSets));
245244
settings.IsWrapResultSetsEnabled = wrapResultSets;
245+
246+
bool batchVersionedData = PropertiesHelper.GetBoolean(Environment.BatchVersionedData, properties, false);
247+
log.Debug("Batch versioned data: " + EnabledDisabled(batchVersionedData));
248+
settings.IsBatchVersionedDataEnabled = batchVersionedData;
249+
246250
settings.BatcherFactory = CreateBatcherFactory(properties, settings.AdoBatchSize, connectionProvider);
247251

248252
string isolationString = PropertiesHelper.GetString(Environment.Isolation, properties, String.Empty);

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -616,16 +616,9 @@ protected SqlString VersionSelectString
616616
get { return sqlVersionSelectString; }
617617
}
618618

619-
public bool IsBatchable
620-
{
621-
get
622-
{
623-
return
624-
OptimisticLockMode == Versioning.OptimisticLock.None
625-
|| (!IsVersioned && OptimisticLockMode == Versioning.OptimisticLock.Version);
626-
//|| Factory.Settings.IsJdbcBatchVersionedData();
627-
}
628-
}
619+
public bool IsBatchable => OptimisticLockMode == Versioning.OptimisticLock.None ||
620+
(!IsVersioned && OptimisticLockMode == Versioning.OptimisticLock.Version) ||
621+
Factory.Settings.IsBatchVersionedDataEnabled;
629622

630623
public virtual string[] QuerySpaces
631624
{

src/NHibernate/nhibernate-configuration.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<xs:enumeration value="query.query_model_rewriter_factory" />
122122
<xs:enumeration value="linqtohql.generatorsregistry" />
123123
<xs:enumeration value="odbc.explicit_datetime_scale" />
124+
<xs:enumeration value="adonet.batch_versioned_data" />
124125
</xs:restriction>
125126
</xs:simpleType>
126127
</xs:attribute>

0 commit comments

Comments
 (0)