LiteDB: Transaction cancelled when index does not exist

If there is a query inside a transaction, and index on the field is not created yet, then after query executes transaction is in the Canceled state.

That happens because when there is no index, IndexNotFoundException is thrown first, and inner transaction in DbEngine.Find is rolled back (and outer transaction cancelled). Upper in the stack exception get caught and index created, and query succeeds. But outer transaction could not be committed because it’s cancelled.

Test to reproduce:

    [TestMethod]
    public void TestIndexCreationInsideTransaction()
    {
        using (var tmp = new TempFile())
        {
            using (var db = new LiteDatabase(tmp.ConnectionString))
            {
                var col = db.GetCollection("col");

                using (var trans = db.BeginTrans())
                {
                    col.Insert(new BsonDocument().Add("test", 0));

                    var query = Query.EQ("test", 0);
                    // Index is created here
                    col.Find(query).ToList();

                    // Fails with TransactionCanceledException
                    trans.Commit();
                }
            }
        }
    }

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

All clear. I understand now. Thanks for clearing this up

Hi @baSSiLL, I will try to take a look on this. I’m out of time for last days. Transactions will be die 😃 To be thread safe, next version will not support transactions (ACID will be only in document level).