Skip to content

Commit d0f6cde

Browse files
Fix closing cases
1 parent 61a2e9d commit d0f6cde

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/AsyncGenerator.yml

Lines changed: 5 additions & 1 deletion
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

src/NHibernate/Async/Impl/StatelessSessionImpl.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,24 @@ public async Task ManagedFlushAsync(CancellationToken cancellationToken)
322322

323323
#region IStatelessSession Members
324324

325+
public async Task ManagedCloseAsync(CancellationToken cancellationToken)
326+
{
327+
cancellationToken.ThrowIfCancellationRequested();
328+
using (BeginContext())
329+
{
330+
if (IsClosed)
331+
{
332+
throw new SessionException("Session was already closed!");
333+
}
334+
// We need to flush the batcher. Otherwise it may have pending operations which will never reach the database,
335+
// although a stateless session is not supposed to retain anything in memory and so should not need any explicit
336+
// flush from users.
337+
await (FlushAsync(cancellationToken)).ConfigureAwait(false);
338+
CloseConnectionManager();
339+
SetClosed();
340+
}
341+
}
342+
325343
/// <summary> Insert a entity.</summary>
326344
/// <param name="entity">A new transient instance </param>
327345
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>

src/NHibernate/Impl/StatelessSessionImpl.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ public void ManagedClose()
438438
{
439439
throw new SessionException("Session was already closed!");
440440
}
441+
// We need to flush the batcher. Otherwise it may have pending operations which will never reach the database,
442+
// although a stateless session is not supposed to retain anything in memory and so should not need any explicit
443+
// flush from users.
444+
Flush();
441445
CloseConnectionManager();
442446
SetClosed();
443447
}
@@ -786,6 +790,10 @@ public void Dispose()
786790
// with a null ref rather than silently leaking a session. And then fix the synchronization.
787791
if (TransactionContext != null && TransactionContext.CanFlushOnSystemTransactionCompleted)
788792
{
793+
// We need to flush the batcher. Otherwise it may have pending operations which will never reach the database,
794+
// although a stateless session is not supposed to retain anything in memory and so should not need any explicit
795+
// flush from users.
796+
Flush();
789797
TransactionContext.ShouldCloseSessionOnSystemTransactionCompleted = true;
790798
return;
791799
}

0 commit comments

Comments
 (0)