Skip to content

NH-3771 - Implement BatchVersionedData factory setting #606

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 2 commits into from
Apr 26, 2017
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Data;
using System.Data.Common;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NUnit.Framework;
using NUnit.Framework.Constraints;

namespace NHibernate.Test.NHSpecificTest.NH1553.MsSQL
{
Expand Down Expand Up @@ -64,18 +64,15 @@ public void UpdateConflictDetectedByNH()
p2.IdentificationNumber += 2;

SavePerson(p1);
Assert.AreEqual(person.Version + 1, p1.Version);
try
{
SavePerson(p2);
Assert.Fail("Expecting stale object state exception");
}
catch (StaleObjectStateException sose)
{
Assert.AreEqual(typeof (Person).FullName, sose.EntityName);
Assert.AreEqual(p2.Id, sose.Identifier);
// as expected.
}
Assert.That(p1.Version, Is.EqualTo(person.Version + 1));

var expectedException = sessions.Settings.IsBatchVersionedDataEnabled
? (IResolveConstraint) Throws.InstanceOf<StaleStateException>()
: Throws.InstanceOf<StaleObjectStateException>()
.And.Property("EntityName").EqualTo(typeof(Person).FullName)
.And.Property("Identifier").EqualTo(p2.Id);

Assert.That(() => SavePerson(p2), expectedException);
}

/// <summary>
Expand All @@ -89,46 +86,43 @@ public void UpdateConflictDetectedBySQLServer()

p1.IdentificationNumber++;

using (ISession session1 = OpenSession())
using (var session1 = OpenSession())
using (var tr1 = BeginTransaction(session1))
{
using (ITransaction tr1 = BeginTransaction(session1))
session1.SaveOrUpdate(p1);
session1.Flush();

using (var session2 = OpenSession())
using (var tr2 = BeginTransaction(session2))
{
session1.SaveOrUpdate(p1);
session1.Flush();
var p2 = session2.Get<Person>(person.Id);
p2.IdentificationNumber += 2;

tr1.Commit();
Assert.That(p1.Version, Is.EqualTo(person.Version + 1));

session2.SaveOrUpdate(p2);

var expectedException = sessions.Settings.IsBatchVersionedDataEnabled
? (IConstraint) Throws.InstanceOf<StaleStateException>()
: Throws.InstanceOf<StaleObjectStateException>()
.And.Property("EntityName").EqualTo(typeof(Person).FullName)
.And.Property("Identifier").EqualTo(p2.Id);

using (ISession session2 = OpenSession())
{
using (ITransaction tr2 = BeginTransaction(session2))
Assert.That(
() =>
{
var p2 = session2.Get<Person>(person.Id);
p2.IdentificationNumber += 2;

tr1.Commit();
Assert.AreEqual(person.Version + 1, p1.Version);

try
{
session2.SaveOrUpdate(p2);
session2.Flush();

tr2.Commit();
Assert.Fail("StaleObjectStateException expected");
}
catch (StaleObjectStateException sose)
{
Assert.AreEqual(typeof (Person).FullName, sose.EntityName);
Assert.AreEqual(p2.Id, sose.Identifier);
// as expected
}
}
}
session2.Flush();
tr2.Commit();
},
expectedException);
}
}
}

protected override bool AppliesTo(Dialect.Dialect dialect)
{
return dialect is MsSql2005Dialect || dialect is MsSql2008Dialect;
return dialect is MsSql2005Dialect;
}

private void SetAllowSnapshotIsolation(bool on)
Expand Down
80 changes: 80 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3771/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Collections;
using NHibernate.AdoNet;
using NHibernate.Cfg;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3771
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.BatchVersionedData, "true");
configuration.SetProperty(Environment.FormatSql, "false");
configuration.SetProperty(Environment.GenerateStatistics, "true");
configuration.SetProperty(Environment.BatchSize, "10");
}

protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)
{
return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory);
}

[Test]
[Description("Should be two batchs with two sentences each.")]
public void InsertAndUpdateWithBatch()
{
sessions.Statistics.Clear();

using (var sqlLog = new SqlLogSpy())
using (ISession s = sessions.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
Singer vs1 = new Singer();
vs1.Id = 1;
vs1.Name = "Fabrizio De Andre";
s.Save(vs1);

Singer vs2 = new Singer();
vs2.Id = 2;
vs2.Name = "Vinicio Capossela";
s.Save(vs2);

s.Flush();

vs1.Name = "De Andre, Fabrizio";
vs2.Name = "Capossela, Vinicio";

s.Flush();

string log = sqlLog.GetWholeLog();

string[] separator = { System.Environment.NewLine };
string[] lines = log.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);

int batchs = 0;
int sqls = 0;
int batchCommands = 0;
foreach (string line in lines)
{
if (line.StartsWith("NHibernate.SQL") && !line.StartsWith("NHibernate.SQL Batch commands:"))
sqls++;

if (line.StartsWith("NHibernate.SQL Batch commands:"))
batchs++;

if (line.StartsWith("command"))
batchCommands++;
}

Assert.AreEqual(2, batchs);
Assert.AreEqual(0, sqls);
Assert.AreEqual(4, batchCommands);
Assert.AreEqual(2, sessions.Statistics.PrepareStatementCount);

tx.Rollback();
}
}
}
}
14 changes: 14 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3771/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH3771">

<class name="Singer">
<id name="Id"/>
<version name="Version"/>
<property name="Name" type="String" />
</class>


</hibernate-mapping>
12 changes: 12 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3771/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH3771
{
public class Singer
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual int Version { get; set; }
}
}
14 changes: 12 additions & 2 deletions src/NHibernate.Test/NHSpecificTest/OptimisticConcurrencyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ public void StaleObjectStateCheckWithNormalizedEntityPersister()
}

top.Name = "new name";
Assert.Throws<StaleObjectStateException>(() => session.Flush());

var expectedException = sessions.Settings.IsBatchVersionedDataEnabled
? Throws.InstanceOf<StaleStateException>()
: Throws.InstanceOf<StaleObjectStateException>();

Assert.That(() => session.Flush(), expectedException);
}
}
finally
Expand Down Expand Up @@ -89,7 +94,12 @@ public void StaleObjectStateCheckWithEntityPersisterAndOptimisticLock()
}

optimistic.String = "new string";
Assert.Throws<StaleObjectStateException>(() => session.Flush());

var expectedException = sessions.Settings.IsBatchVersionedDataEnabled
? Throws.InstanceOf<StaleStateException>()
: Throws.InstanceOf<StaleObjectStateException>();

Assert.That(() => session.Flush(), expectedException);
}
}
finally
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,8 @@
<Compile Include="NHSpecificTest\NH3634\PersonMapper.cs" />
<Compile Include="NHSpecificTest\NH3727\Entity.cs" />
<Compile Include="NHSpecificTest\NH3727\FixtureByCode.cs" />
<Compile Include="NHSpecificTest\NH3771\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3771\Model.cs" />
<Compile Include="NHSpecificTest\NH3795\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3844\Domain.cs" />
<Compile Include="NHSpecificTest\NH3844\Fixture.cs" />
Expand Down Expand Up @@ -3269,6 +3271,7 @@
<EmbeddedResource Include="NHSpecificTest\NH2204\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3874\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3771\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2218\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3046\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3518\Mappings.hbm.xml" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,26 @@ public void ShouldCheckStaleState()

try
{
using (ISession session = OpenSession())
using (var session = OpenSession())
{
session.Save(versioned);
session.Flush();

using (ISession concurrentSession = OpenSession())
using (var concurrentSession = OpenSession())
{
var sameVersioned = concurrentSession.Get<SimpleVersioned>(versioned.Id);
sameVersioned.Something = "another string";
concurrentSession.Flush();
}

versioned.Something = "new string";
session.Flush();

var expectedException = sessions.Settings.IsBatchVersionedDataEnabled
? Throws.InstanceOf<StaleStateException>()
: Throws.InstanceOf<StaleObjectStateException>();

Assert.That(() => session.Flush(), expectedException);
}
Assert.Fail("Expected exception was not thrown");
}
catch (StaleObjectStateException)
{
// as expected
}
finally
{
Expand Down
3 changes: 1 addition & 2 deletions src/NHibernate/Cfg/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ public static string Version
// Unused, not implemented
public const string StatementFetchSize = "jdbc.fetch_size";

public const string BatchVersionedData = "jdbc.batch_versioned_data";

// Unused, not implemented
public const string OutputStylesheet = "xml.output_stylesheet";

Expand Down Expand Up @@ -154,6 +152,7 @@ public static string Version
// Unused, not implemented
public const string SqlExceptionConverter = "sql_exception_converter";

public const string BatchVersionedData = "adonet.batch_versioned_data";
public const string WrapResultSets = "adonet.wrap_result_sets";
public const string BatchSize = "adonet.batch_size";
public const string BatchStrategy = "adonet.factory_class";
Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate/Cfg/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public Settings()
#region JDBC Specific (Not Ported)

//private int jdbcFetchSize;
//private bool isJdbcBatchVersionedData;

#endregion
public SqlStatementLogger SqlStatementLogger { get; internal set; }
Expand Down Expand Up @@ -118,6 +117,8 @@ public Settings()

public bool IsNamedQueryStartupCheckingEnabled { get; internal set; }

public bool IsBatchVersionedDataEnabled { get; internal set; }

#region NH specific

public IsolationLevel IsolationLevel { get; internal set; }
Expand Down
6 changes: 5 additions & 1 deletion src/NHibernate/Cfg/SettingsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ public Settings BuildSettings(IDictionary<string, string> properties)

//ADO.NET and connection settings:

// TODO: Environment.BatchVersionedData
settings.AdoBatchSize = PropertiesHelper.GetInt32(Environment.BatchSize, properties, 0);
bool orderInserts = PropertiesHelper.GetBoolean(Environment.OrderInserts, properties, (settings.AdoBatchSize > 0));
log.Info("Order SQL inserts for batching: " + EnabledDisabled(orderInserts));
Expand All @@ -243,6 +242,11 @@ public Settings BuildSettings(IDictionary<string, string> properties)
bool wrapResultSets = PropertiesHelper.GetBoolean(Environment.WrapResultSets, properties, false);
log.Debug("Wrap result sets: " + EnabledDisabled(wrapResultSets));
settings.IsWrapResultSetsEnabled = wrapResultSets;

bool batchVersionedData = PropertiesHelper.GetBoolean(Environment.BatchVersionedData, properties, false);
log.Debug("Batch versioned data: " + EnabledDisabled(batchVersionedData));
settings.IsBatchVersionedDataEnabled = batchVersionedData;

settings.BatcherFactory = CreateBatcherFactory(properties, settings.AdoBatchSize, connectionProvider);

string isolationString = PropertiesHelper.GetString(Environment.Isolation, properties, String.Empty);
Expand Down
13 changes: 3 additions & 10 deletions src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,16 +616,9 @@ protected SqlString VersionSelectString
get { return sqlVersionSelectString; }
}

public bool IsBatchable
{
get
{
return
OptimisticLockMode == Versioning.OptimisticLock.None
|| (!IsVersioned && OptimisticLockMode == Versioning.OptimisticLock.Version);
//|| Factory.Settings.IsJdbcBatchVersionedData();
}
}
public bool IsBatchable => OptimisticLockMode == Versioning.OptimisticLock.None ||
(!IsVersioned && OptimisticLockMode == Versioning.OptimisticLock.Version) ||
Factory.Settings.IsBatchVersionedDataEnabled;

public virtual string[] QuerySpaces
{
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 @@ -121,6 +121,7 @@
<xs:enumeration value="query.query_model_rewriter_factory" />
<xs:enumeration value="linqtohql.generatorsregistry" />
<xs:enumeration value="odbc.explicit_datetime_scale" />
<xs:enumeration value="adonet.batch_versioned_data" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
Expand Down