Skip to content

Commit 305b89a

Browse files
Fix querying cases
1 parent d0f6cde commit 305b89a

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/NHibernate/Async/Impl/StatelessSessionImpl.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public override async Task ListAsync(IQueryExpression queryExpression, QueryPara
9595
cancellationToken.ThrowIfCancellationRequested();
9696
using (BeginProcess())
9797
{
98+
// We need to flush the batcher. Otherwise it may have pending operations which will not already have reached the database,
99+
// and the query may yield stale data.
100+
await (FlushAsync(cancellationToken)).ConfigureAwait(false);
101+
98102
queryParameters.ValidateParameters();
99103
var plan = GetHQLQueryPlan(queryExpression, false);
100104

@@ -127,6 +131,10 @@ public override async Task<IList<T>> ListAsync<T>(CriteriaImpl criteria, Cancell
127131
cancellationToken.ThrowIfCancellationRequested();
128132
using (BeginProcess())
129133
{
134+
// We need to flush the batcher. Otherwise it may have pending operations which will not already have reached the database,
135+
// and the query may yield stale data.
136+
await (FlushAsync(cancellationToken)).ConfigureAwait(false);
137+
130138
string[] implementors = Factory.GetImplementors(criteria.EntityOrClassName);
131139
int size = implementors.Length;
132140

@@ -252,6 +260,10 @@ public override async Task ListCustomQueryAsync(ICustomQuery customQuery, QueryP
252260
cancellationToken.ThrowIfCancellationRequested();
253261
using (BeginProcess())
254262
{
263+
// We need to flush the batcher. Otherwise it may have pending operations which will not already have reached the database,
264+
// and the query may yield stale data.
265+
await (FlushAsync(cancellationToken)).ConfigureAwait(false);
266+
255267
var loader = new CustomLoader(customQuery, Factory);
256268

257269
var success = false;
@@ -488,6 +500,10 @@ public async Task ManagedCloseAsync(CancellationToken cancellationToken)
488500
cancellationToken.ThrowIfCancellationRequested();
489501
using (BeginProcess())
490502
{
503+
// We need to flush the batcher. Otherwise it may have pending operations which will not already have reached the database,
504+
// and the get may miss an entity which should be there.
505+
await (FlushAsync(cancellationToken)).ConfigureAwait(false);
506+
491507
object result = await (Factory.GetEntityPersister(entityName).LoadAsync(id, null, lockMode ?? LockMode.None, this, cancellationToken)).ConfigureAwait(false);
492508
if (temporaryPersistenceContext.IsLoadFinished)
493509
{
@@ -563,6 +579,10 @@ public async Task ManagedCloseAsync(CancellationToken cancellationToken)
563579
cancellationToken.ThrowIfCancellationRequested();
564580
using (BeginProcess())
565581
{
582+
// We need to flush the batcher. Otherwise it may have pending operations which will not already have reached the database,
583+
// and the query may yield stale data.
584+
await (FlushAsync(cancellationToken)).ConfigureAwait(false);
585+
566586
IEntityPersister persister = GetEntityPersister(entityName, entity);
567587
object id = persister.GetIdentifier(entity);
568588
if (log.IsDebugEnabled())

src/NHibernate/Impl/StatelessSessionImpl.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ public override void List(IQueryExpression queryExpression, QueryParameters quer
110110
{
111111
using (BeginProcess())
112112
{
113+
// We need to flush the batcher. Otherwise it may have pending operations which will not already have reached the database,
114+
// and the query may yield stale data.
115+
Flush();
116+
113117
queryParameters.ValidateParameters();
114118
var plan = GetHQLQueryPlan(queryExpression, false);
115119

@@ -140,6 +144,10 @@ public override IList<T> List<T>(CriteriaImpl criteria)
140144
{
141145
using (BeginProcess())
142146
{
147+
// We need to flush the batcher. Otherwise it may have pending operations which will not already have reached the database,
148+
// and the query may yield stale data.
149+
Flush();
150+
143151
string[] implementors = Factory.GetImplementors(criteria.EntityOrClassName);
144152
int size = implementors.Length;
145153

@@ -267,6 +275,10 @@ public override void ListCustomQuery(ICustomQuery customQuery, QueryParameters q
267275
{
268276
using (BeginProcess())
269277
{
278+
// We need to flush the batcher. Otherwise it may have pending operations which will not already have reached the database,
279+
// and the query may yield stale data.
280+
Flush();
281+
270282
var loader = new CustomLoader(customQuery, Factory);
271283

272284
var success = false;
@@ -351,7 +363,7 @@ public override bool IsOpen
351363

352364
public override FlushMode FlushMode
353365
{
354-
get { return FlushMode.Commit; }
366+
get { return FlushMode.Always; }
355367
set { throw new NotSupportedException(); }
356368
}
357369

@@ -568,6 +580,10 @@ public object Get(string entityName, object id, LockMode lockMode)
568580
{
569581
using (BeginProcess())
570582
{
583+
// We need to flush the batcher. Otherwise it may have pending operations which will not already have reached the database,
584+
// and the get may miss an entity which should be there.
585+
Flush();
586+
571587
object result = Factory.GetEntityPersister(entityName).Load(id, null, lockMode ?? LockMode.None, this);
572588
if (temporaryPersistenceContext.IsLoadFinished)
573589
{
@@ -631,6 +647,10 @@ public void Refresh(string entityName, object entity, LockMode lockMode)
631647
{
632648
using (BeginProcess())
633649
{
650+
// We need to flush the batcher. Otherwise it may have pending operations which will not already have reached the database,
651+
// and the query may yield stale data.
652+
Flush();
653+
634654
IEntityPersister persister = GetEntityPersister(entityName, entity);
635655
object id = persister.GetIdentifier(entity);
636656
if (log.IsDebugEnabled())

0 commit comments

Comments
 (0)