Skip to content

Commit

Permalink
Fixed issue mbdavid#1772: Unfortunately, the TransactionService for a…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
frankneumann committed Dec 26, 2020
1 parent af3181e commit 48d89fd
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion LiteDB/Engine/Services/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public TransactionService(HeaderPage header, LockService locker, DiskService dis
_reader = _disk.GetReader();
}

/// <summary>
/// Finalizer: Will be called once a thread is closed. The TransactionMonitor._slot releases the used TransactionService.
/// </summary>
~TransactionService()
{
Dispose(false);
}

/// <summary>
/// Create (or get from transaction-cache) snapshot and return
/// </summary>
Expand Down Expand Up @@ -375,10 +383,22 @@ IEnumerable<PageBuffer> source()
}

/// <summary>
/// Dispose
/// Public implementation of Dispose pattern.
/// </summary>
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
Expand Down Expand Up @@ -409,6 +429,12 @@ public void Dispose()
_reader.Dispose();

_state = TransactionState.Disposed;

if (!dispose)
{
// Remove transaction monitor's dictionary
_monitor.ReleaseTransaction(this);
}
}
}
}

0 comments on commit 48d89fd

Please sign in to comment.