diff --git a/LiteDB/Engine/Services/TransactionService.cs b/LiteDB/Engine/Services/TransactionService.cs
index 5f03d71df..0a03e5837 100644
--- a/LiteDB/Engine/Services/TransactionService.cs
+++ b/LiteDB/Engine/Services/TransactionService.cs
@@ -74,6 +74,14 @@ public TransactionService(HeaderPage header, LockService locker, DiskService dis
_reader = _disk.GetReader();
}
+ ///
+ /// Finalizer: Will be called once a thread is closed. The TransactionMonitor._slot releases the used TransactionService.
+ ///
+ ~TransactionService()
+ {
+ Dispose(false);
+ }
+
///
/// Create (or get from transaction-cache) snapshot and return
///
@@ -375,10 +383,22 @@ IEnumerable source()
}
///
- /// Dispose
+ /// Public implementation of Dispose pattern.
///
public void Dispose()
{
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ // Protected implementation of Dispose pattern.
+ protected virtual void Dispose(bool dispose)
+ {
+ if (_state == TransactionState.Disposed)
+ {
+ return;
+ }
+
ENSURE(_state != TransactionState.Disposed, "transaction must be active before call Done");
// clean snapshots if there is no commit/rollback
@@ -409,6 +429,12 @@ public void Dispose()
_reader.Dispose();
_state = TransactionState.Disposed;
+
+ if (!dispose)
+ {
+ // Remove transaction monitor's dictionary
+ _monitor.ReleaseTransaction(this);
+ }
}
}
}
\ No newline at end of file