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

Add transaction scope and distributed transaction tests #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
@@ -0,0 +1,126 @@
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using NHibernate.Envers.Configuration;
using NHibernate.Envers.Tests.Entities;
using NUnit.Framework;

namespace NHibernate.Envers.Tests.Integration.Basic
{
public class TransactionScopeWithUseConnectionOnPrepare : TestBase
{
public TransactionScopeWithUseConnectionOnPrepare(AuditStrategyForTest strategyType) : base(strategyType)
{
}

#if NETCOREAPP2_0
[Ignore("System.Transactions is not supported in Core. See https://github.com/dotnet/corefx/issues/19894")]
#endif
[Test]
public void ShouldNotThrowWhenDistributedTransaction()
{
var entity = new StrTestEntity { Str = "data" };

Assert.DoesNotThrow(() =>
{
using (var distrTx = new TransactionScope())
{
// Simulation of durable resource enlistment
using (var newSession = Session.SessionFactory.OpenSession())
{
newSession.FlushMode = FlushMode.Manual;
newSession.Query<StrTestEntity>().FirstOrDefault();
}

using (var newSession2 = Session.SessionFactory.OpenSession())
{
newSession2.FlushMode = FlushMode.Manual;
newSession2.Save(entity);
newSession2.Flush();
}

distrTx.Complete();
}
});
}

#if NETCOREAPP2_0
[Ignore("System.Transactions is not supported in Core. See https://github.com/dotnet/corefx/issues/19894")]
#endif
[Test]
public void ShouldNotThrowWhenDistributedTransactionWithAutoFlushMode()
{
var entity = new StrTestEntity { Str = "data" };

Assert.DoesNotThrow(() =>
{
using (var distrTx = new TransactionScope())
{
using (var newSession = Session.SessionFactory.OpenSession())
{
newSession.FlushMode = FlushMode.Auto;
newSession.Query<StrTestEntity>().FirstOrDefault();
}

using (var newSession2 = Session.SessionFactory.OpenSession())
{
newSession2.FlushMode = FlushMode.Auto;
newSession2.Save(entity);
newSession2.Flush();
}

distrTx.Complete();
}
});
}

#if NETCOREAPP2_0
[Ignore("System.Transactions is not supported in Core. See https://github.com/dotnet/corefx/issues/19894")]
#endif
[Test]
public void ShouldNotThrowWhenDistributedTransactionWithNhTransaction()
{
var entity = new StrTestEntity { Str = "data" };

Assert.DoesNotThrow(() =>
{
using (var distrTx = new TransactionScope())
{
using (var newSession = Session.SessionFactory.OpenSession())
{
newSession.Query<StrTestEntity>().FirstOrDefault();
}

using (var newSession2 = Session.SessionFactory.OpenSession())
{
using (var nhTx = newSession2.BeginTransaction())
{
newSession2.Save(entity);
newSession2.Flush();

// There is no problem in this case,
// because BeforeTransactionCompletion is called right after this line (before 1st commit phase of distributed transaction)
nhTx.Commit();
}
}

distrTx.Complete();
}
});
}

protected override void Initialize()
{
}

protected override void AddToConfiguration(Cfg.Configuration configuration)
{
ConfigurationKey.StoreDataAtDelete.SetUserValue(configuration, true);
}

protected override IEnumerable<string> Mappings
{
get { return new[] {"Entities.Mapping.hbm.xml", "Integration.Collection.NoRevision.Mapping.hbm.xml"}; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using NHibernate.Envers.Tests.Entities;
using NUnit.Framework;
using System.Transactions;

namespace NHibernate.Envers.Tests.Integration.Basic
{
public class TransactionScopeWithoutUseConnectionOnPrepare : TransactionScopeWithUseConnectionOnPrepare
{
public TransactionScopeWithoutUseConnectionOnPrepare(AuditStrategyForTest strategyType) : base(strategyType)
{
}

#if NETCOREAPP2_0
[Ignore("System.Transactions is not supported in Core. See https://github.com/dotnet/corefx/issues/19894")]
#endif
[Test]
public void ShouldNotThrow()
{
var entity = new StrTestEntity { Str = "data" };

Assert.DoesNotThrow(() =>
{
using (var distrTx = new TransactionScope())
{
using (var newSession = Session.SessionFactory.OpenSession())
{
newSession.FlushMode = FlushMode.Manual;
newSession.Save(entity);
newSession.Flush();
}

distrTx.Complete();
}
});
}

protected override void AddToConfiguration(Cfg.Configuration configuration)
{
// It is recommended since NH 5
configuration.SetProperty(NHibernate.Cfg.Environment.UseConnectionOnSystemTransactionPrepare, "false");

base.AddToConfiguration(configuration);
}
}
}