Skip to content

Commit 3d3ed91

Browse files
Fix stateless batcher (#2755)
Fix #2750
1 parent 161ff3c commit 3d3ed91

File tree

8 files changed

+483
-2
lines changed

8 files changed

+483
-2
lines changed

src/AsyncGenerator.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@
6060
name: BestGuessEntityName
6161
containingTypeName: ISessionImplementor
6262
- conversion: Ignore
63-
name: Contains
63+
name: CloseSessionFromSystemTransaction
6464
containingTypeName: ISessionImplementor
65+
# TODO 6.0: Remove ignore rule for IStatelessSession.Close
66+
- conversion: Ignore
67+
name: Close
68+
containingTypeName: IStatelessSession
6569
- conversion: Ignore
6670
name: GetUnsavedVersionValue
6771
containingTypeName: UnsavedValueFactory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using System.Transactions;
15+
using NHibernate.Cfg;
16+
using NHibernate.Cfg.MappingSchema;
17+
using NHibernate.Mapping.ByCode;
18+
using NUnit.Framework;
19+
using NHibernate.Linq;
20+
21+
namespace NHibernate.Test.NHSpecificTest.GH2750
22+
{
23+
using System.Threading.Tasks;
24+
[TestFixture]
25+
public class ByCodeFixtureAsync : TestCaseMappingByCode
26+
{
27+
protected override HbmMapping GetMappings()
28+
{
29+
var mapper = new ModelMapper();
30+
mapper.Class<TestEntity>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.Assigned)); });
31+
32+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
33+
}
34+
35+
protected override void Configure(Configuration configuration)
36+
{
37+
configuration.SetProperty(Cfg.Environment.BatchSize, 10.ToString());
38+
configuration.SetProperty(Cfg.Environment.UseConnectionOnSystemTransactionPrepare, true.ToString());
39+
}
40+
41+
protected override void OnTearDown()
42+
{
43+
using (var session = OpenSession())
44+
using (var transaction = session.BeginTransaction())
45+
{
46+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
47+
48+
transaction.Commit();
49+
}
50+
}
51+
52+
[Test]
53+
public async Task ShouldWorkWithOuterSystemTransactionAsync()
54+
{
55+
if (!Sfi.ConnectionProvider.Driver.SupportsSystemTransactions)
56+
Assert.Ignore("System.Transactions support is required by this test");
57+
58+
const int count = 3;
59+
var entities = new List<TestEntity>(count);
60+
for (var i = 0; i < count; i++)
61+
{
62+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
63+
}
64+
65+
using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
66+
{
67+
using (var session = Sfi.OpenStatelessSession())
68+
{
69+
foreach (var entity in entities)
70+
{
71+
await (session.InsertAsync(entity));
72+
}
73+
}
74+
75+
transaction.Complete();
76+
}
77+
78+
using (var session = OpenSession())
79+
{
80+
var results = await (session.QueryOver<TestEntity>().ListAsync<TestEntity>());
81+
82+
Assert.That(results.Count, Is.EqualTo(count));
83+
}
84+
}
85+
86+
[Test]
87+
public async Task ShouldWorkWithInnerSystemTransactionAsync()
88+
{
89+
if (!Sfi.ConnectionProvider.Driver.SupportsSystemTransactions)
90+
Assert.Ignore("System.Transactions support is required by this test");
91+
92+
const int count = 3;
93+
var entities = new List<TestEntity>(count);
94+
for (var i = 0; i < count; i++)
95+
{
96+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
97+
}
98+
99+
using (var session = Sfi.OpenStatelessSession())
100+
{
101+
using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
102+
{
103+
foreach (var entity in entities)
104+
{
105+
await (session.InsertAsync(entity));
106+
}
107+
await (session.FlushBatcherAsync());
108+
109+
transaction.Complete();
110+
}
111+
}
112+
113+
using (var session = OpenSession())
114+
{
115+
var results = await (session.QueryOver<TestEntity>().ListAsync<TestEntity>());
116+
117+
Assert.That(results.Count, Is.EqualTo(count));
118+
}
119+
}
120+
121+
[Test]
122+
public async Task ShouldWorkWithoutTransactionAsync()
123+
{
124+
const int count = 3;
125+
var entities = new List<TestEntity>(count);
126+
for (var i = 0; i < count; i++)
127+
{
128+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
129+
}
130+
131+
using (var session = Sfi.OpenStatelessSession())
132+
{
133+
foreach (var entity in entities)
134+
{
135+
await (session.InsertAsync(entity));
136+
}
137+
}
138+
139+
using (var session = OpenSession())
140+
{
141+
var results = await (session.QueryOver<TestEntity>().ListAsync<TestEntity>());
142+
143+
Assert.That(results.Count, Is.EqualTo(count));
144+
}
145+
}
146+
147+
[Test]
148+
public async Task ShouldGetEntityAfterInsertAsync()
149+
{
150+
var entity = new TestEntity { Id = Guid.NewGuid() };
151+
using (var session = Sfi.OpenStatelessSession())
152+
{
153+
await (session.InsertAsync(entity));
154+
Assert.That(await (session.GetAsync<TestEntity>(entity.Id)), Is.Not.Null);
155+
}
156+
}
157+
158+
[Test]
159+
public async Task ShouldQueryEntityAfterInsertAsync()
160+
{
161+
var entity = new TestEntity { Id = Guid.NewGuid() };
162+
using (var session = Sfi.OpenStatelessSession())
163+
{
164+
await (session.InsertAsync(entity));
165+
Assert.That(await (session.Query<TestEntity>().FirstOrDefaultAsync(e => e.Id == entity.Id)), Is.Not.Null);
166+
}
167+
}
168+
169+
[Test]
170+
public async Task ShouldQueryOverEntityAfterInsertAsync()
171+
{
172+
var entity = new TestEntity { Id = Guid.NewGuid() };
173+
using (var session = Sfi.OpenStatelessSession())
174+
{
175+
await (session.InsertAsync(entity));
176+
Assert.That(await (session.QueryOver<TestEntity>().Where(e => e.Id == entity.Id).SingleOrDefaultAsync()), Is.Not.Null);
177+
}
178+
}
179+
180+
[Test]
181+
public async Task ShouldHqlQueryEntityAfterInsertAsync()
182+
{
183+
var entity = new TestEntity { Id = Guid.NewGuid() };
184+
using (var session = Sfi.OpenStatelessSession())
185+
{
186+
await (session.InsertAsync(entity));
187+
Assert.That(await (session.CreateQuery("from TestEntity where Id = :id").SetGuid("id", entity.Id).UniqueResultAsync()), Is.Not.Null);
188+
}
189+
}
190+
}
191+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2750
4+
{
5+
public class TestEntity
6+
{
7+
public virtual Guid Id { get; set; }
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Transactions;
5+
using NHibernate.Cfg;
6+
using NHibernate.Cfg.MappingSchema;
7+
using NHibernate.Mapping.ByCode;
8+
using NUnit.Framework;
9+
10+
namespace NHibernate.Test.NHSpecificTest.GH2750
11+
{
12+
[TestFixture]
13+
public class ByCodeFixture : TestCaseMappingByCode
14+
{
15+
protected override HbmMapping GetMappings()
16+
{
17+
var mapper = new ModelMapper();
18+
mapper.Class<TestEntity>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.Assigned)); });
19+
20+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
21+
}
22+
23+
protected override void Configure(Configuration configuration)
24+
{
25+
configuration.SetProperty(Cfg.Environment.BatchSize, 10.ToString());
26+
configuration.SetProperty(Cfg.Environment.UseConnectionOnSystemTransactionPrepare, true.ToString());
27+
}
28+
29+
protected override void OnTearDown()
30+
{
31+
using (var session = OpenSession())
32+
using (var transaction = session.BeginTransaction())
33+
{
34+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
35+
36+
transaction.Commit();
37+
}
38+
}
39+
40+
[Test]
41+
public void ShouldWorkWithOuterSystemTransaction()
42+
{
43+
if (!Sfi.ConnectionProvider.Driver.SupportsSystemTransactions)
44+
Assert.Ignore("System.Transactions support is required by this test");
45+
46+
const int count = 3;
47+
var entities = new List<TestEntity>(count);
48+
for (var i = 0; i < count; i++)
49+
{
50+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
51+
}
52+
53+
using (var transaction = new TransactionScope())
54+
{
55+
using (var session = Sfi.OpenStatelessSession())
56+
{
57+
foreach (var entity in entities)
58+
{
59+
session.Insert(entity);
60+
}
61+
}
62+
63+
transaction.Complete();
64+
}
65+
66+
using (var session = OpenSession())
67+
{
68+
var results = session.QueryOver<TestEntity>().List<TestEntity>();
69+
70+
Assert.That(results.Count, Is.EqualTo(count));
71+
}
72+
}
73+
74+
[Test]
75+
public void ShouldWorkWithInnerSystemTransaction()
76+
{
77+
if (!Sfi.ConnectionProvider.Driver.SupportsSystemTransactions)
78+
Assert.Ignore("System.Transactions support is required by this test");
79+
80+
const int count = 3;
81+
var entities = new List<TestEntity>(count);
82+
for (var i = 0; i < count; i++)
83+
{
84+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
85+
}
86+
87+
using (var session = Sfi.OpenStatelessSession())
88+
{
89+
using (var transaction = new TransactionScope())
90+
{
91+
foreach (var entity in entities)
92+
{
93+
session.Insert(entity);
94+
}
95+
session.FlushBatcher();
96+
97+
transaction.Complete();
98+
}
99+
}
100+
101+
using (var session = OpenSession())
102+
{
103+
var results = session.QueryOver<TestEntity>().List<TestEntity>();
104+
105+
Assert.That(results.Count, Is.EqualTo(count));
106+
}
107+
}
108+
109+
[Test]
110+
public void ShouldWorkWithoutTransaction()
111+
{
112+
const int count = 3;
113+
var entities = new List<TestEntity>(count);
114+
for (var i = 0; i < count; i++)
115+
{
116+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
117+
}
118+
119+
using (var session = Sfi.OpenStatelessSession())
120+
{
121+
foreach (var entity in entities)
122+
{
123+
session.Insert(entity);
124+
}
125+
}
126+
127+
using (var session = OpenSession())
128+
{
129+
var results = session.QueryOver<TestEntity>().List<TestEntity>();
130+
131+
Assert.That(results.Count, Is.EqualTo(count));
132+
}
133+
}
134+
135+
[Test]
136+
public void ShouldGetEntityAfterInsert()
137+
{
138+
var entity = new TestEntity { Id = Guid.NewGuid() };
139+
using (var session = Sfi.OpenStatelessSession())
140+
{
141+
session.Insert(entity);
142+
Assert.That(session.Get<TestEntity>(entity.Id), Is.Not.Null);
143+
}
144+
}
145+
146+
[Test]
147+
public void ShouldQueryEntityAfterInsert()
148+
{
149+
var entity = new TestEntity { Id = Guid.NewGuid() };
150+
using (var session = Sfi.OpenStatelessSession())
151+
{
152+
session.Insert(entity);
153+
Assert.That(session.Query<TestEntity>().FirstOrDefault(e => e.Id == entity.Id), Is.Not.Null);
154+
}
155+
}
156+
157+
[Test]
158+
public void ShouldQueryOverEntityAfterInsert()
159+
{
160+
var entity = new TestEntity { Id = Guid.NewGuid() };
161+
using (var session = Sfi.OpenStatelessSession())
162+
{
163+
session.Insert(entity);
164+
Assert.That(session.QueryOver<TestEntity>().Where(e => e.Id == entity.Id).SingleOrDefault(), Is.Not.Null);
165+
}
166+
}
167+
168+
[Test]
169+
public void ShouldHqlQueryEntityAfterInsert()
170+
{
171+
var entity = new TestEntity { Id = Guid.NewGuid() };
172+
using (var session = Sfi.OpenStatelessSession())
173+
{
174+
session.Insert(entity);
175+
Assert.That(session.CreateQuery("from TestEntity where Id = :id").SetGuid("id", entity.Id).UniqueResult(), Is.Not.Null);
176+
}
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)