Database file is inexplicably locked during SQLite commit

后端 未结 9 739
深忆病人
深忆病人 2020-12-31 12:17

I\'m performing a large number of INSERTS to a SQLite database. I\'m using just one thread. I batch the writes to improve performance and have a bit of security in case of

9条回答
  •  情话喂你
    2020-12-31 13:12

    We had a very similar problem using nested Transactions with the TransactionScope class. We thought all database actions occurred on the same thread...however we were caught out by the Transaction mechanism...more specifically the Ambient transaction.

    Basically there was a transaction higher up the chain which, by the magic of ado, the connection automatically enlisted in. The result was that, even though we thought we were writing to the database on a single thread, the write didn't really happen until the topmost transaction was committed. At this 'indeterminate' point the database was written to causing it to be locked outside of our control.

    The solution was to ensure that the sqlite database did not directly take part in the ambient transaction by ensuring we used something like:

    using(TransactionScope scope = new TransactionScope(TransactionScopeOptions.RequiresNew))
    {
      ...
      scope.Complete()
    }
    

提交回复
热议问题