Skip to content

Commit b5e0607

Browse files
committed
Merge pull request #409 from nhibernate/NH-3583
NH-3583 - Fix AutoFlush inside TransactionScope
2 parents edcf928 + e50d058 commit b5e0607

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Linq;
2+
using System.Transactions;
3+
using NHibernate.Cfg.MappingSchema;
4+
using NHibernate.Linq;
5+
using NHibernate.Mapping.ByCode;
6+
using NUnit.Framework;
7+
8+
namespace NHibernate.Test.NHSpecificTest.NH3583
9+
{
10+
public class AutoFlushFixture : TestCaseMappingByCode
11+
{
12+
protected override HbmMapping GetMappings()
13+
{
14+
var mapper = new ModelMapper();
15+
mapper.Class<Entity>(rc =>
16+
{
17+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
18+
rc.Property(x => x.Name);
19+
});
20+
21+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
22+
}
23+
24+
protected override void OnTearDown()
25+
{
26+
using (var session = OpenSession())
27+
using (var transaction = session.BeginTransaction())
28+
{
29+
session.Delete("from System.Object");
30+
31+
session.Flush();
32+
transaction.Commit();
33+
}
34+
}
35+
36+
[Test]
37+
public void ShouldAutoFlushWhenInExplicitTransaction()
38+
{
39+
using (var session = OpenSession())
40+
using (session.BeginTransaction())
41+
{
42+
var e1 = new Entity { Name = "Bob" };
43+
session.Save(e1);
44+
45+
var result = (from e in session.Query<Entity>()
46+
where e.Name == "Bob"
47+
select e).ToList();
48+
49+
Assert.That(result.Count, Is.EqualTo(1));
50+
}
51+
}
52+
[Test]
53+
public void ShouldAutoFlushWhenInDistributedTransaction()
54+
{
55+
using (new TransactionScope())
56+
using (var session = OpenSession())
57+
{
58+
var e1 = new Entity { Name = "Bob" };
59+
session.Save(e1);
60+
61+
var result = (from e in session.Query<Entity>()
62+
where e.Name == "Bob"
63+
select e).ToList();
64+
65+
Assert.That(result.Count, Is.EqualTo(1));
66+
}
67+
}
68+
}
69+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3583
4+
{
5+
class Entity
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
}
10+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,8 @@
715715
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
716716
<Compile Include="Linq\ByMethod\DistinctTests.cs" />
717717
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
718+
<Compile Include="NHSpecificTest\NH3583\Entity.cs" />
719+
<Compile Include="NHSpecificTest\NH3583\AutoFlushFixture.cs" />
718720
<Compile Include="NHSpecificTest\NH3372\Entity.cs" />
719721
<Compile Include="NHSpecificTest\NH3372\Fixture.cs" />
720722
<Compile Include="NHSpecificTest\NH3453\Classes.cs" />

src/NHibernate/Impl/SessionImpl.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ private bool AutoFlushIfRequired(ISet<string> querySpaces)
11241124
using (new SessionIdLoggingContext(SessionId))
11251125
{
11261126
CheckAndUpdateSessionStatus();
1127-
if (!TransactionInProgress)
1127+
if (!ConnectionManager.IsInActiveTransaction)
11281128
{
11291129
// do not auto-flush while outside a transaction
11301130
return false;
@@ -1467,10 +1467,7 @@ public override void Flush()
14671467

14681468
public override bool TransactionInProgress
14691469
{
1470-
get
1471-
{
1472-
return !IsClosed && Transaction.IsActive;
1473-
}
1470+
get { return ConnectionManager.IsInActiveTransaction; }
14741471
}
14751472

14761473
public bool IsDirty()

0 commit comments

Comments
 (0)