Skip to content

Commit

Permalink
Don't allow commit or rollback unless we are in a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
mlockett42 committed Jan 27, 2024
1 parent 34cdae5 commit 317c742
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions LiteDBAsync/LiteDatabaseAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class LiteDatabaseAsync : ILiteDatabaseAsync
private static DefaultDictionary<ILiteDatabase, int> _wrappedDatabases = new DefaultDictionary<ILiteDatabase, int>();
private static object _hashSetLock = new object();
private bool _isClosedTransaction = false;
private bool _isTransaction;

/// <summary>
/// Starts LiteDB database using a connection string for file system database
Expand All @@ -38,6 +39,7 @@ public LiteDatabaseAsync(ConnectionString connectionString, BsonMapper mapper =
RecordUnderlyingDatabaseInMap(UnderlyingDatabase);
_backgroundThread = new Thread(BackgroundLoop);
_backgroundThread.Start();
_isTransaction = false;
}

internal void VerifyNoClosedTransaction()
Expand All @@ -61,6 +63,7 @@ public LiteDatabaseAsync(Stream stream, BsonMapper mapper = null, Stream logStre
RecordUnderlyingDatabaseInMap(UnderlyingDatabase);
_backgroundThread = new Thread(BackgroundLoop);
_backgroundThread.Start();
_isTransaction = false;
}

/// <summary>
Expand All @@ -75,6 +78,7 @@ public LiteDatabaseAsync(ILiteDatabase wrappedDatabase, bool disposeOfWrappedDat
_backgroundThread = new Thread(BackgroundLoop);
_backgroundThread.Start();
_disposeOfWrappedDatabase = disposeOfWrappedDatabase;
_isTransaction = false;
}

private static void RecordUnderlyingDatabaseInMap(ILiteDatabase underlyingDatabase)
Expand Down Expand Up @@ -104,6 +108,7 @@ private LiteDatabaseAsync(ILiteDatabaseAsync sourceDatabaseAsync, bool disposeOf
_backgroundThread = new Thread(BackgroundLoop);
_backgroundThread.Start();
_disposeOfWrappedDatabase = disposeOfWrappedDatabase;
_isTransaction = true;
}

/// <summary>
Expand Down Expand Up @@ -247,6 +252,10 @@ public ILiteStorageAsync<TFileId> GetStorage<TFileId>(string filesCollection = "
/// </summary>
public async Task<bool> CommitAsync()
{
if (_isTransaction == false)
{
throw new LiteAsyncException("Not a transaction, commit not allowed.");
}
var result = await EnqueueAsync(
() => UnderlyingDatabase.Commit());
_isClosedTransaction = true;
Expand All @@ -258,6 +267,10 @@ public async Task<bool> CommitAsync()
/// </summary>
public Task<bool> RollbackAsync()
{
if (_isTransaction == false)
{
throw new LiteAsyncException("Not a transaction, rollback not allowed.");
}
var result = EnqueueAsync(
() => UnderlyingDatabase.Rollback());
_isClosedTransaction = true;
Expand Down
44 changes: 44 additions & 0 deletions LiteDBAsyncTest/Engine/Transaction_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,49 @@ public async Task Transactions_Out_Of_Order_Disposes_Are_OK()
asyncDb.Dispose();
transDb.Dispose();
}

[Fact]
public async Task Test_Rollback_Fails_If_Not_In_Transaction()
{
var data1 = DataGen.Person(1, 100).ToArray();

var connectionString = new ConnectionString()
{
Filename = Path.Combine(Path.GetTempPath(), "litedbn-async-testing-" + Path.GetRandomFileName() + ".db"),
Connection = ConnectionType.Shared,
Password = "hunter2"
};

using var asyncDb1 = new LiteDatabaseAsync(connectionString);

var asyncPerson1 = asyncDb1.GetCollection<Person>();
await asyncPerson1.InsertAsync(data1);

var exception = await Assert.ThrowsAsync<LiteAsyncException>(asyncDb1.RollbackAsync);

Assert.Equal("Not a transaction, rollback not allowed.", exception.Message);
}

[Fact]
public async Task Test_Commit_Fails_If_Not_In_Transaction()
{
var data1 = DataGen.Person(1, 100).ToArray();

var connectionString = new ConnectionString()
{
Filename = Path.Combine(Path.GetTempPath(), "litedbn-async-testing-" + Path.GetRandomFileName() + ".db"),
Connection = ConnectionType.Shared,
Password = "hunter2"
};

using var asyncDb1 = new LiteDatabaseAsync(connectionString);

var asyncPerson1 = asyncDb1.GetCollection<Person>();
await asyncPerson1.InsertAsync(data1);

var exception = await Assert.ThrowsAsync<LiteAsyncException>(asyncDb1.CommitAsync);

Assert.Equal("Not a transaction, commit not allowed.", exception.Message);
}
}
}

0 comments on commit 317c742

Please sign in to comment.