litedb-async icon indicating copy to clipboard operation
litedb-async copied to clipboard

Transactions rollback does not work, even explicit

Open YMichurin opened this issue 1 year ago • 1 comments

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

YMichurin avatar Jan 09 '24 06:01 YMichurin

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.

mlockett42 avatar Jan 26 '24 12:01 mlockett42

Fixed in Version v0.1.18

mlockett42 avatar Jun 26 '24 13:06 mlockett42