Skip to content

Nh 3771 - Batch Update with Optimistic Locking control. #467

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

Closed
wants to merge 10 commits into from
Closed
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
Expand Up @@ -70,11 +70,9 @@ public void UpdateConflictDetectedByNH()
SavePerson(p2);
Assert.Fail("Expecting stale object state exception");
}
catch (StaleObjectStateException sose)
catch (StaleStateException)
{
Assert.AreEqual(typeof (Person).FullName, sose.EntityName);
Assert.AreEqual(p2.Id, sose.Identifier);
// as expected.
// as expected
}
}

Expand Down Expand Up @@ -114,10 +112,8 @@ public void UpdateConflictDetectedBySQLServer()
tr2.Commit();
Assert.Fail("StaleObjectStateException expected");
}
catch (StaleObjectStateException sose)
catch (StaleStateException)
{
Assert.AreEqual(typeof (Person).FullName, sose.EntityName);
Assert.AreEqual(p2.Id, sose.Identifier);
// as expected
}
}
Expand Down Expand Up @@ -178,4 +174,4 @@ protected override void Configure(Configuration configuration)
typeof (SQLUpdateConflictToStaleStateExceptionConverter).AssemblyQualifiedName);
}
}
}
}
79 changes: 79 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3771/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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.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);

s.Transaction.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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void StaleObjectStateCheckWithNormalizedEntityPersister()
}

top.Name = "new name";
Assert.Throws<StaleObjectStateException>(() => session.Flush());
Assert.Throws(Is.InstanceOf<StaleStateException>(), () => session.Flush());
}
}
finally
Expand Down Expand Up @@ -89,7 +89,8 @@ public void StaleObjectStateCheckWithEntityPersisterAndOptimisticLock()
}

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

Assert.Throws(Is.InstanceOf<StaleStateException>(), () => session.Flush());
}
}
finally
Expand All @@ -102,4 +103,4 @@ public void StaleObjectStateCheckWithEntityPersisterAndOptimisticLock()
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,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 @@ -3198,6 +3200,7 @@
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="NHSpecificTest\NH3771\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2204\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3874\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\Mappings.hbm.xml" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void ShouldCheckStaleState()
}
Assert.Fail("Expected exception was not thrown");
}
catch (StaleObjectStateException)
catch (StaleStateException)
{
// as expected
}
Expand Down
8 changes: 1 addition & 7 deletions src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,7 @@ protected SqlString VersionSelectString

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

public virtual string[] QuerySpaces
Expand Down