-
Notifications
You must be signed in to change notification settings - Fork 934
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" }; } | ||
} | ||
|
||
[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(); | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
} | ||
} |
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; | ||
} | ||
} |
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> |
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"}; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
} | ||
|
||
[Test] | ||
public void SecondTransactionShouldntBeCommitted() | ||
{ | ||
|
@@ -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(); | ||
} | ||
} | ||
|
||
|
@@ -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(); | ||
} | ||
} | ||
} | ||
} |
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); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.