Skip to content
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

Change cascade style for DefaultDirtyCheckEventListener to persist to avoid flushing the session #2752

Merged
merged 4 commits into from
May 15, 2021
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
23 changes: 13 additions & 10 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1413/FixtureByCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


using NHibernate.Cfg.MappingSchema;
using NHibernate.Engine;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

Expand All @@ -26,7 +27,7 @@ protected override HbmMapping GetMappings()
var mapper = new ModelMapper();
mapper.Class<EntityParent>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Native));
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
rc.Property(x => x.Name);
rc.Bag(x => x.Children, m =>
{
Expand All @@ -38,7 +39,7 @@ protected override HbmMapping GetMappings()

mapper.Class<EntityChild>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Native));
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
rc.Property(x => x.Name);
});

Expand Down Expand Up @@ -74,13 +75,12 @@ protected override void OnTearDown()
}
}

[Test]
[KnownBug("#1413")]
public async Task SessionIsDirtyShouldNotTriggerCascadeSavingAsync()
[Theory]
public async Task SessionIsDirtyShouldNotTriggerCascadeSavingAsync(bool beginTransaction)
{
Sfi.Statistics.IsStatisticsEnabled = true;
using (var session = OpenSession())
using (session.BeginTransaction())
using (beginTransaction ? session.BeginTransaction() : null)
{
var parent = await (GetParentAsync(session));
var entityChild = new EntityChild
Expand All @@ -95,11 +95,14 @@ public async Task SessionIsDirtyShouldNotTriggerCascadeSavingAsync()
var isDirty = await (session.IsDirtyAsync());

Assert.That(Sfi.Statistics.EntityInsertCount, Is.EqualTo(0), "Dirty has triggered an insert");
Assert.That(
entityChild.Id,
Is.EqualTo(0),
"Transient objects should not be saved by ISession.IsDirty() call (expected 0 as Id)");
Assert.That(isDirty, "ISession.IsDirty() call should return true.");
if (Dialect.SupportsIdentityColumns)
{
Assert.That(
entityChild.Id,
Is.EqualTo(0),
"Transient objects should not be saved by ISession.IsDirty() call (expected 0 as Id)");
}
}
}

Expand Down
23 changes: 13 additions & 10 deletions src/NHibernate.Test/NHSpecificTest/GH1413/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NHibernate.Cfg.MappingSchema;
using NHibernate.Engine;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

Expand All @@ -14,7 +15,7 @@ protected override HbmMapping GetMappings()
var mapper = new ModelMapper();
mapper.Class<EntityParent>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Native));
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
rc.Property(x => x.Name);
rc.Bag(x => x.Children, m =>
{
Expand All @@ -26,7 +27,7 @@ protected override HbmMapping GetMappings()

mapper.Class<EntityChild>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Native));
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
rc.Property(x => x.Name);
});

Expand Down Expand Up @@ -62,13 +63,12 @@ protected override void OnTearDown()
}
}

[Test]
[KnownBug("#1413")]
public void SessionIsDirtyShouldNotTriggerCascadeSaving()
[Theory]
public void SessionIsDirtyShouldNotTriggerCascadeSaving(bool beginTransaction)
{
Sfi.Statistics.IsStatisticsEnabled = true;
using (var session = OpenSession())
using (session.BeginTransaction())
using (beginTransaction ? session.BeginTransaction() : null)
{
var parent = GetParent(session);
var entityChild = new EntityChild
Expand All @@ -83,11 +83,14 @@ public void SessionIsDirtyShouldNotTriggerCascadeSaving()
var isDirty = session.IsDirty();

Assert.That(Sfi.Statistics.EntityInsertCount, Is.EqualTo(0), "Dirty has triggered an insert");
Assert.That(
entityChild.Id,
Is.EqualTo(0),
"Transient objects should not be saved by ISession.IsDirty() call (expected 0 as Id)");
Assert.That(isDirty, "ISession.IsDirty() call should return true.");
if (Dialect.SupportsIdentityColumns)
{
Assert.That(
entityChild.Id,
Is.EqualTo(0),
"Transient objects should not be saved by ISession.IsDirty() call (expected 0 as Id)");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@


using System;
using NHibernate.Engine;
using NHibernate.Util;

namespace NHibernate.Event.Default
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using NHibernate.Engine;
using NHibernate.Util;

namespace NHibernate.Event.Default
{
Expand All @@ -11,6 +13,10 @@ public partial class DefaultDirtyCheckEventListener : AbstractFlushingEventListe
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(DefaultDirtyCheckEventListener));

protected override object Anything => IdentityMap.Instantiate(10);

protected override CascadingAction CascadingAction => CascadingAction.Persist;

public virtual void OnDirtyCheck(DirtyCheckEvent @event)
{
int oldSize = @event.Session.ActionQueue.CollectionRemovalsCount;
Expand Down