Skip to content

NH-4034 - flushing sessions sharing transaction on commit. #648

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
Jun 29, 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
45 changes: 29 additions & 16 deletions src/NHibernate.Test/DebugSessionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Threading;
using log4net;
using NHibernate.Cache;
Expand Down Expand Up @@ -53,29 +54,41 @@ public bool CheckSessionsWereClosed()
var allClosed = true;
foreach (var session in _openedSessions)
{
if (session.IsOpen)
// Do not inverse, we want to close all of them.
allClosed = CheckSessionWasClosed(session) && allClosed;
// Catches only session opened from another one while sharing the connection. Those
// opened without sharing the connection stay un-monitored.
foreach (var dependentSession in session.ConnectionManager.DependentSessions.ToList())
{
if (session.TransactionContext?.ShouldCloseSessionOnDistributedTransactionCompleted ?? false)
{
// Delayed transactions not having completed and closed their sessions? Give them a chance to complete.
Thread.Sleep(100);
if (!session.IsOpen)
{
_log.Warn($"Test case had a delayed close of session {session.SessionId}.");
continue;
}
}

_log.Error($"Test case didn't close session {session.SessionId}, closing");
allClosed = false;
(session as ISession)?.Close();
(session as IStatelessSession)?.Close();
allClosed = CheckSessionWasClosed(dependentSession) && allClosed;
}
}

return allClosed;
}

private bool CheckSessionWasClosed(ISessionImplementor session)
{
if (!session.IsOpen)
return true;

if (session.TransactionContext?.ShouldCloseSessionOnDistributedTransactionCompleted ?? false)
{
// Delayed transactions not having completed and closed their sessions? Give them a chance to complete.
Thread.Sleep(100);
if (!session.IsOpen)
{
_log.Warn($"Test case had a delayed close of session {session.SessionId}.");
return true;
}
}

_log.Error($"Test case didn't close session {session.SessionId}, closing");
(session as ISession)?.Close();
(session as IStatelessSession)?.Close();
return false;
}

ISessionBuilder ISessionFactory.WithOptions()
{
return new SessionBuilder(ActualFactory.WithOptions(), this);
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 @@ -1464,6 +1464,8 @@
<Compile Include="TestDialects\PostgreSQL83TestDialect.cs" />
<Compile Include="TestDialects\SQLiteTestDialect.cs" />
<Compile Include="Tools\hbm2ddl\SchemaExportTests\ExportToFileFixture.cs" />
<Compile Include="TransactionTest\Person.cs" />
<Compile Include="TransactionTest\TransactionFixtureBase.cs" />
<Compile Include="TransformTests\ImplementationOfEqualityTests.cs" />
<Compile Include="TypesTest\CharClass.cs" />
<Compile Include="TypesTest\CharClassFixture.cs" />
Expand Down Expand Up @@ -3286,6 +3288,7 @@
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="TransactionTest\Person.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2241\Mappings.hbm.xml" />
<EmbeddedResource Include="Futures\Mappings.hbm.xml" />
<EmbeddedResource Include="SessionBuilder\Mappings.hbm.xml" />
Expand Down
92 changes: 81 additions & 11 deletions src/NHibernate.Test/SystemTransactions/TransactionFixture.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using NHibernate.DomainModel;
using NHibernate.Linq;
using NHibernate.Test.TransactionTest;
using NUnit.Framework;

namespace NHibernate.Test.SystemTransactions
{
[TestFixture]
public class TransactionFixture : TestCase
public class TransactionFixture : TransactionFixtureBase
{
protected override IList Mappings
{
get { return new string[] { "WZ.hbm.xml" }; }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a locally defined class for having more freedom changing it for the needs of the tests.

}

[Test]
public void CanUseSystemTransactionsToCommit()
{
object identifier;
int identifier;
using(ISession session = Sfi.OpenSession())
using(TransactionScope tx = new TransactionScope())
{
W s = new W();
var s = new Person();
session.Save(s);
identifier = s.Id;
tx.Complete();
Expand All @@ -29,11 +26,84 @@ public void CanUseSystemTransactionsToCommit()
using (ISession session = Sfi.OpenSession())
using (TransactionScope tx = new TransactionScope())
{
W w = session.Get<W>(identifier);
var w = session.Get<Person>(identifier);
Assert.IsNotNull(w);
session.Delete(w);
tx.Complete();
}
}

[Test]
public void FlushFromTransactionAppliesToDisposedSharingSession()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we should no more support that pattern someday, but since it is currently supported, better test it.

{
var flushOrder = new List<int>();
using (var s = OpenSession(new TestInterceptor(0, flushOrder)))
{
var builder = s.SessionWithOptions().Connection();

using (var t = new TransactionScope())
{
var p1 = new Person();
var p2 = new Person();
var p3 = new Person();
var p4 = new Person();

using (var s1 = builder.Interceptor(new TestInterceptor(1, flushOrder)).OpenSession())
s1.Save(p1);
using (var s2 = builder.Interceptor(new TestInterceptor(2, flushOrder)).OpenSession())
{
s2.Save(p2);
using (var s3 = s2.SessionWithOptions().Connection().Interceptor(new TestInterceptor(3, flushOrder)).OpenSession())
s3.Save(p3);
}
s.Save(p4);
t.Complete();
}
}

Assert.That(flushOrder, Is.EqualTo(new[] { 0, 1, 2, 3 }));

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
Assert.That(s.Query<Person>().Count(), Is.EqualTo(4));
t.Commit();
}
}

[Test]
public void FlushFromTransactionAppliesToSharingSession()
{
var flushOrder = new List<int>();
using (var s = OpenSession(new TestInterceptor(0, flushOrder)))
{
var builder = s.SessionWithOptions().Connection();

using (var s1 = builder.Interceptor(new TestInterceptor(1, flushOrder)).OpenSession())
using (var s2 = builder.Interceptor(new TestInterceptor(2, flushOrder)).OpenSession())
using (var s3 = s2.SessionWithOptions().Connection().Interceptor(new TestInterceptor(3, flushOrder)).OpenSession())
using (var t = new TransactionScope())
{
var p1 = new Person();
var p2 = new Person();
var p3 = new Person();
var p4 = new Person();
s1.Save(p1);
s2.Save(p2);
s3.Save(p3);
s.Save(p4);
t.Complete();
}
}

Assert.That(flushOrder, Is.EqualTo(new[] { 0, 1, 2, 3 }));

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
Assert.That(s.Query<Person>().Count(), Is.EqualTo(4));
t.Commit();
}
}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/TransactionTest/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.TransactionTest
{
public class Person
{
public virtual int Id { get; set; }

public virtual DateTime CreatedAt { get; set; } = DateTime.Now;
}
}
12 changes: 12 additions & 0 deletions src/NHibernate.Test/TransactionTest/Person.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernate.Test.TransactionTest"
assembly="NHibernate.Test">

<class name="Person">
<id name="Id">
<generator class="hilo"/>
</id>
<property name="CreatedAt"/>
</class>
</hibernate-mapping>
55 changes: 43 additions & 12 deletions src/NHibernate.Test/TransactionTest/TransactionFixture.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using System;
using System.Collections;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Linq;
using NUnit.Framework;

namespace NHibernate.Test.TransactionTest
{
[TestFixture]
public class TransactionFixture : TestCase
public class TransactionFixture : TransactionFixtureBase
{
protected override IList Mappings
{
// The mapping is only actually needed in one test
get { return new string[] {"Simple.hbm.xml"}; }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Simple entity is anything but simple. It does not have a mapped id property, just a column in database, causing weird patterns when actually willing to use it.

}

[Test]
public void SecondTransactionShouldntBeCommitted()
{
Expand Down Expand Up @@ -74,25 +70,25 @@ public void CommandAfterTransactionShouldWork()
{
using (ISession s = OpenSession())
{
using (ITransaction t = s.BeginTransaction())
using (s.BeginTransaction())
{
}

s.CreateQuery("from Simple").List();
s.CreateQuery("from Person").List();

using (ITransaction t = s.BeginTransaction())
{
t.Commit();
}

s.CreateQuery("from Simple").List();
s.CreateQuery("from Person").List();

using (ITransaction t = s.BeginTransaction())
{
t.Rollback();
}

s.CreateQuery("from Simple").List();
s.CreateQuery("from Person").List();
}
}

Expand Down Expand Up @@ -141,5 +137,40 @@ public void WasCommittedOrRolledBack()
}
}
}

[Test]
public void FlushFromTransactionAppliesToSharingSession()
{
var flushOrder = new List<int>();
using (var s = OpenSession(new TestInterceptor(0, flushOrder)))
{
var builder = s.SessionWithOptions().Connection();

using (var s1 = builder.Interceptor(new TestInterceptor(1, flushOrder)).OpenSession())
using (var s2 = builder.Interceptor(new TestInterceptor(2, flushOrder)).OpenSession())
using (var s3 = s1.SessionWithOptions().Connection().Interceptor(new TestInterceptor(3, flushOrder)).OpenSession())
using (var t = s.BeginTransaction())
{
var p1 = new Person();
var p2 = new Person();
var p3 = new Person();
var p4 = new Person();
s1.Save(p1);
s2.Save(p2);
s3.Save(p3);
s.Save(p4);
t.Commit();
}
}

Assert.That(flushOrder, Is.EqualTo(new[] { 0, 1, 2, 3 }));

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
Assert.That(s.Query<Person>().Count(), Is.EqualTo(4));
t.Commit();
}
}
}
}
39 changes: 39 additions & 0 deletions src/NHibernate.Test/TransactionTest/TransactionFixtureBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;

namespace NHibernate.Test.TransactionTest
{
public abstract class TransactionFixtureBase : TestCase
{
protected override IList Mappings => new[] { "TransactionTest.Person.hbm.xml" };

protected override string MappingsAssembly => "NHibernate.Test";

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.Delete("from System.Object");
t.Commit();
}
}

public class TestInterceptor : EmptyInterceptor
{
private readonly int _numero;
private readonly List<int> _flushOrder;

public TestInterceptor(int numero, List<int> flushOrder)
{
_numero = numero;
_flushOrder = flushOrder;
}

public override void PreFlush(ICollection entitites)
{
_flushOrder.Add(_numero);
}
}
}
}
Loading