Skip to content

Commit 48d89fd

Browse files
committed
Fixed issue #1772: Unfortunately, the TransactionService for a closed thread was not be removed from the _transactions dictionary within the TransactionMonitor.
Therefore a finalizer for the TransactionService was added, which takes care of removing the transaction from the _transactions dictionary.
1 parent af3181e commit 48d89fd

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

LiteDB/Engine/Services/TransactionService.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ public TransactionService(HeaderPage header, LockService locker, DiskService dis
7474
_reader = _disk.GetReader();
7575
}
7676

77+
/// <summary>
78+
/// Finalizer: Will be called once a thread is closed. The TransactionMonitor._slot releases the used TransactionService.
79+
/// </summary>
80+
~TransactionService()
81+
{
82+
Dispose(false);
83+
}
84+
7785
/// <summary>
7886
/// Create (or get from transaction-cache) snapshot and return
7987
/// </summary>
@@ -375,10 +383,22 @@ IEnumerable<PageBuffer> source()
375383
}
376384

377385
/// <summary>
378-
/// Dispose
386+
/// Public implementation of Dispose pattern.
379387
/// </summary>
380388
public void Dispose()
381389
{
390+
Dispose(true);
391+
GC.SuppressFinalize(this);
392+
}
393+
394+
// Protected implementation of Dispose pattern.
395+
protected virtual void Dispose(bool dispose)
396+
{
397+
if (_state == TransactionState.Disposed)
398+
{
399+
return;
400+
}
401+
382402
ENSURE(_state != TransactionState.Disposed, "transaction must be active before call Done");
383403

384404
// clean snapshots if there is no commit/rollback
@@ -409,6 +429,12 @@ public void Dispose()
409429
_reader.Dispose();
410430

411431
_state = TransactionState.Disposed;
432+
433+
if (!dispose)
434+
{
435+
// Remove transaction monitor's dictionary
436+
_monitor.ReleaseTransaction(this);
437+
}
412438
}
413439
}
414440
}

0 commit comments

Comments
 (0)