Skip to content

Commit 3f1f025

Browse files
committed
Move InitializeEntitiesAndCollections to ProcessResults
1 parent eaf9782 commit 3f1f025

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
@@ -108,8 +108,9 @@ public async Task<int> ProcessResultsSetAsync(DbDataReader reader, CancellationT
108108
await (reader.NextResultAsync(cancellationToken)).ConfigureAwait(false);
109109
}
110110

111-
await (InitializeEntitiesAndCollectionsAsync(reader, hydratedObjects, cancellationToken)).ConfigureAwait(false);
112-
111+
StopLoadingCollections(reader);
112+
_reader = reader;
113+
_hydratedObjects = hydratedObjects;
113114
return rowCount;
114115
}
115116
}
@@ -123,19 +124,5 @@ public async Task ExecuteNonBatchedAsync(CancellationToken cancellationToken)
123124
}
124125

125126
protected abstract Task<IList<TResult>> GetResultsNonBatchedAsync(CancellationToken cancellationToken);
126-
127-
private async Task InitializeEntitiesAndCollectionsAsync(DbDataReader reader, List<object>[] hydratedObjects, CancellationToken cancellationToken)
128-
{
129-
cancellationToken.ThrowIfCancellationRequested();
130-
for (var i = 0; i < _queryInfos.Count; i++)
131-
{
132-
var queryInfo = _queryInfos[i];
133-
if (queryInfo.IsResultFromCache)
134-
continue;
135-
await (queryInfo.Loader.InitializeEntitiesAndCollectionsAsync(
136-
hydratedObjects[i], reader, Session, queryInfo.Parameters.IsReadOnly(Session),
137-
queryInfo.CacheBatcher, cancellationToken)).ConfigureAwait(false);
138-
}
139-
}
140127
}
141128
}

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");
@@ -398,5 +400,17 @@ public override string ToString()
398400
{
399401
return base.ToString() + "<rs=" + ResultSet + ">";
400402
}
403+
404+
internal void StopLoadingCollections(ICollectionPersister[] collectionPersisters)
405+
{
406+
foreach (var collectionKey in localLoadingCollectionKeys)
407+
{
408+
var loadingCollectionEntry = LoadContext.LocateLoadingCollectionEntry(collectionKey);
409+
if (loadingCollectionEntry != null && Array.IndexOf(collectionPersisters, loadingCollectionEntry.Persister) >= 0)
410+
{
411+
loadingCollectionEntry.StopLoading = true;
412+
}
413+
}
414+
}
401415
}
402416
}

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
@@ -672,6 +672,15 @@ internal void InitializeEntitiesAndCollections(
672672
}
673673
}
674674

675+
internal void StopLoadingCollections(ISessionImplementor session, DbDataReader reader)
676+
{
677+
var collectionPersisters = CollectionPersisters;
678+
if (collectionPersisters == null || collectionPersisters.Length == 0)
679+
return;
680+
681+
session.PersistenceContext.LoadContexts.GetCollectionLoadContext(reader).StopLoadingCollections(collectionPersisters);
682+
}
683+
675684
private void EndCollectionLoad(DbDataReader reader, ISessionImplementor session, ICollectionPersister collectionPersister)
676685
{
677686
//this is a query and we are loading multiple instances of the same collection role

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
2628
{
@@ -247,8 +249,9 @@ public int ProcessResultsSet(DbDataReader reader)
247249
reader.NextResult();
248250
}
249251

250-
InitializeEntitiesAndCollections(reader, hydratedObjects);
251-
252+
StopLoadingCollections(reader);
253+
_reader = reader;
254+
_hydratedObjects = hydratedObjects;
252255
return rowCount;
253256
}
254257
}
@@ -258,6 +261,7 @@ public void ProcessResults()
258261
{
259262
ThrowIfNotInitialized();
260263

264+
InitializeEntitiesAndCollections(_reader, _hydratedObjects);
261265
for (var i = 0; i < _queryInfos.Count; i++)
262266
{
263267
var queryInfo = _queryInfos[i];
@@ -329,6 +333,17 @@ private void InitializeEntitiesAndCollections(DbDataReader reader, List<object>[
329333
}
330334
}
331335

336+
private void StopLoadingCollections(DbDataReader reader)
337+
{
338+
for (var i = 0; i < _queryInfos.Count; i++)
339+
{
340+
var queryInfo = _queryInfos[i];
341+
if (queryInfo.IsResultFromCache)
342+
continue;
343+
queryInfo.Loader.StopLoadingCollections(Session, reader);
344+
}
345+
}
346+
332347
private void ThrowIfNotInitialized()
333348
{
334349
if (_queryInfos == null)

0 commit comments

Comments
 (0)