litedb-async
litedb-async copied to clipboard
Transactions rollback does not work, even explicit
I was not able to make the code work (console app on .net 8, LiteDb.Async 0.1.7)
var db = new LiteDatabaseAsync("litedbDemo.db");
try
{
var col = db.GetCollection<MyEntity>();
await db.BeginTransactionAsync();
await col.UpsertAsync(new MyEntity());
throw new Exception("something goes wrong");
}
catch (Exception)
{
await db.RollbackAsync();
}
whether await db.RollbackAsync(); executes or not, changes made with UpsertAsync stay there in the db.
The moment I made these operations sync (original LiteDb implementation) everything starts working as expected: you do not have to run db.Rollback() even, no changes go to db till the moment you commit the transaction
BeginTransactionAsync creates a new database object which contains the transaction. Try the following -
var db = new LiteDatabaseAsync("litedbDemo.db");
try
{
var trans = await db.BeginTransactionAsync();
var col = trans.GetCollection<MyEntity>();
await col.UpsertAsync(new MyEntity());
throw new Exception("something goes wrong");
}
catch (Exception)
{
await trans.RollbackAsync();
}
var col = db.GetCollection<MyEntity>();
// col should not contain the new MyEntity entry added above.
You don't need to actually RollbackAsync just let trans fall out of scope.
Fixed in Version v0.1.18