Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions LiteDB/Engine/Disk/DiskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Threading;
using LiteDB.Utils;
using static LiteDB.Constants;

namespace LiteDB.Engine
Expand Down Expand Up @@ -55,7 +56,15 @@ public DiskService(
{
LOG($"creating new database: '{Path.GetFileName(_dataFactory.Name)}'", "DISK");

this.Initialize(_dataPool.Writer.Value, settings.Collation, settings.InitialSize);
try
{
this.Initialize(_dataPool.Writer.Value, settings.Collation, settings.InitialSize);
}
catch (Exception ex)
{
LOG($"Error while initializing DiskService: {ex.Message}", "ERROR");
throw;
}
}

// if not readonly, force open writable datafile
Expand Down Expand Up @@ -340,14 +349,16 @@ public void Dispose()
// can change file size
var delete = _logFactory.Exists() && _logPool.Writer.Value.Length == 0;

var tc = new TryCatch();

// dispose Stream pools
_dataPool.Dispose();
_logPool.Dispose();
tc.Catch(() => _dataPool.Dispose());
tc.Catch(() => _logPool.Dispose());

if (delete) _logFactory.Delete();
if (delete) tc.Catch(() => _logFactory.Delete());

// other disposes
_cache.Dispose();
tc.Catch(() => _cache.Dispose());
}
}
}
42 changes: 25 additions & 17 deletions LiteDB/Engine/Services/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,32 +398,40 @@ protected virtual void Dispose(bool dispose)
return;
}

ENSURE(_state != TransactionState.Disposed, "transaction must be active before call Done");

// clean snapshots if there is no commit/rollback
if (_state == TransactionState.Active && _snapshots.Count > 0)
try
{
// release writable snapshots
foreach (var snapshot in _snapshots.Values.Where(x => x.Mode == LockMode.Write))
{
// discard all dirty pages
_disk.DiscardDirtyPages(snapshot.GetWritablePages(true, true).Select(x => x.Buffer));

// discard all clean pages
_disk.DiscardCleanPages(snapshot.GetWritablePages(false, true).Select(x => x.Buffer));
}
ENSURE(_state != TransactionState.Disposed, "transaction must be active before call Done");

// release buffers in read-only snaphosts
foreach (var snapshot in _snapshots.Values.Where(x => x.Mode == LockMode.Read))
// clean snapshots if there is no commit/rollback
if (_state == TransactionState.Active && _snapshots.Count > 0)
{
foreach (var page in snapshot.LocalPages)
// release writable snapshots
foreach (var snapshot in _snapshots.Values.Where(x => x.Mode == LockMode.Write))
{
page.Buffer.Release();
// discard all dirty pages
_disk.DiscardDirtyPages(snapshot.GetWritablePages(true, true).Select(x => x.Buffer));

// discard all clean pages
_disk.DiscardCleanPages(snapshot.GetWritablePages(false, true).Select(x => x.Buffer));
}

snapshot.CollectionPage?.Buffer.Release();
// release buffers in read-only snaphosts
foreach (var snapshot in _snapshots.Values.Where(x => x.Mode == LockMode.Read))
{
foreach (var page in snapshot.LocalPages)
{
page.Buffer.Release();
}

snapshot.CollectionPage?.Buffer.Release();
}
}
}
catch (Exception ex)
{
LOG($"Error while disposing TransactionService: {ex.Message}", "ERROR");
}

_reader.Dispose();

Expand Down