Skip to content

Commit 5f0c449

Browse files
committed
Move InitializeEntitiesAndCollections to ProcessResults
1 parent 1148aa9 commit 5f0c449

File tree

6 files changed

+49
-18
lines changed

6 files changed

+49
-18
lines changed

src/AsyncGenerator.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
applyChanges: true
66
analyzation:
77
methodConversion:
8+
#TODO 6.0: Remove ignore rule for IQueryBatchItem.ProcessResults
9+
- conversion: Ignore
10+
name: ProcessResults
11+
containingTypeName: IQueryBatchItem
812
- conversion: Ignore
913
name: PostProcessInsert
1014
containingTypeName: HqlSqlWalker

src/NHibernate/Async/Multi/QueryBatchItemBase.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ public async Task<int> ProcessResultsSetAsync(DbDataReader reader, CancellationT
118118
await (reader.NextResultAsync(cancellationToken)).ConfigureAwait(false);
119119
}
120120

121-
await (InitializeEntitiesAndCollectionsAsync(reader, hydratedObjects, cancellationToken)).ConfigureAwait(false);
122-
121+
StopLoadingCollections(reader);
122+
_reader = reader;
123+
_hydratedObjects = hydratedObjects;
123124
return rowCount;
124125
}
125126
}
@@ -133,19 +134,5 @@ public async Task ExecuteNonBatchedAsync(CancellationToken cancellationToken)
133134
}
134135

135136
protected abstract Task<IList<TResult>> GetResultsNonBatchedAsync(CancellationToken cancellationToken);
136-
137-
private async Task InitializeEntitiesAndCollectionsAsync(DbDataReader reader, List<object>[] hydratedObjects, CancellationToken cancellationToken)
138-
{
139-
cancellationToken.ThrowIfCancellationRequested();
140-
for (var i = 0; i < _queryInfos.Count; i++)
141-
{
142-
var queryInfo = _queryInfos[i];
143-
if (queryInfo.IsResultFromCache)
144-
continue;
145-
await (queryInfo.Loader.InitializeEntitiesAndCollectionsAsync(
146-
hydratedObjects[i], reader, Session, queryInfo.Parameters.IsReadOnly(Session),
147-
queryInfo.CacheBatcher, cancellationToken)).ConfigureAwait(false);
148-
}
149-
}
150137
}
151138
}

src/NHibernate/Engine/Loading/CollectionLoadContext.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public IPersistentCollection GetLoadingCollection(ICollectionPersister persister
124124
}
125125
else
126126
{
127+
if (loadingCollectionEntry.StopLoading)
128+
return null;
127129
if (loadingCollectionEntry.ResultSet == resultSet)
128130
{
129131
log.Debug("found loading collection bound to current result set processing; reading row");
@@ -416,5 +418,17 @@ public override string ToString()
416418
{
417419
return base.ToString() + "<rs=" + ResultSet + ">";
418420
}
421+
422+
internal void StopLoadingCollections(ICollectionPersister[] collectionPersisters)
423+
{
424+
foreach (var collectionKey in localLoadingCollectionKeys)
425+
{
426+
var loadingCollectionEntry = LoadContext.LocateLoadingCollectionEntry(collectionKey);
427+
if (loadingCollectionEntry != null && Array.IndexOf(collectionPersisters, loadingCollectionEntry.Persister) >= 0)
428+
{
429+
loadingCollectionEntry.StopLoading = true;
430+
}
431+
}
432+
}
419433
}
420434
}

src/NHibernate/Engine/Loading/LoadingCollectionEntry.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public IPersistentCollection Collection
4444
get { return collection; }
4545
}
4646

47+
public bool StopLoading { get; set; }
48+
4749
public override string ToString()
4850
{
4951
return GetType().FullName + "<rs=" + ResultSet + ", coll=" + MessageHelper.InfoString(Persister.Role, Key) + ">@" + Convert.ToString(GetHashCode(), 16);

src/NHibernate/Loader/Loader.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,15 @@ internal void InitializeEntitiesAndCollections(
702702
cacheBatcher.ExecuteBatch();
703703
}
704704

705+
internal void StopLoadingCollections(ISessionImplementor session, DbDataReader reader)
706+
{
707+
var collectionPersisters = CollectionPersisters;
708+
if (collectionPersisters == null || collectionPersisters.Length == 0)
709+
return;
710+
711+
session.PersistenceContext.LoadContexts.GetCollectionLoadContext(reader).StopLoadingCollections(collectionPersisters);
712+
}
713+
705714
private void EndCollectionLoad(DbDataReader reader, ISessionImplementor session, ICollectionPersister collectionPersister,
706715
CacheBatcher cacheBatcher)
707716
{

src/NHibernate/Multi/QueryBatchItemBase.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public abstract partial class QueryBatchItemBase<TResult> : IQueryBatchItem<TRes
2121
private List<QueryInfo> _queryInfos;
2222
private CacheMode? _cacheMode;
2323
private IList<TResult> _finalResults;
24+
private DbDataReader _reader;
25+
private List<object>[] _hydratedObjects;
2426

2527
protected class QueryInfo : ICachingInformation, ICachingInformationWithFetches
2628
{
@@ -260,8 +262,9 @@ public int ProcessResultsSet(DbDataReader reader)
260262
reader.NextResult();
261263
}
262264

263-
InitializeEntitiesAndCollections(reader, hydratedObjects);
264-
265+
StopLoadingCollections(reader);
266+
_reader = reader;
267+
_hydratedObjects = hydratedObjects;
265268
return rowCount;
266269
}
267270
}
@@ -271,6 +274,7 @@ public void ProcessResults()
271274
{
272275
ThrowIfNotInitialized();
273276

277+
InitializeEntitiesAndCollections(_reader, _hydratedObjects);
274278
for (var i = 0; i < _queryInfos.Count; i++)
275279
{
276280
var queryInfo = _queryInfos[i];
@@ -348,6 +352,17 @@ private void InitializeEntitiesAndCollections(DbDataReader reader, List<object>[
348352
}
349353
}
350354

355+
private void StopLoadingCollections(DbDataReader reader)
356+
{
357+
for (var i = 0; i < _queryInfos.Count; i++)
358+
{
359+
var queryInfo = _queryInfos[i];
360+
if (queryInfo.IsResultFromCache)
361+
continue;
362+
queryInfo.Loader.StopLoadingCollections(Session, reader);
363+
}
364+
}
365+
351366
private void ThrowIfNotInitialized()
352367
{
353368
if (_queryInfos == null)

0 commit comments

Comments
 (0)