It seems that many people encountered this problem and couldn't solve it, so i'll write the solution. In System.Data.SQLite
, starting from version 1.0.45.0, closing a connection doesn't automatically close any related DbCommand
or DbDataReader
objects. They must be closed explicitly. Therefore the connection is not really closed, to allow those objects to work, but is just queued to be closed after all related objects are disposed. See this.
In previous versions it behaves as expected, closing a connection actually closes it!
Solution: Dispose all DbCommand
and DbDataReader
Explicitly when you finish with them, just like you do with DbConnection
itself.
Strange SQLite behavior- 'Database is locked' sometimes, but sometimes not
-
23-03-2022 - |
Pergunta
I'm using SQLite (via .NET's wrapper System.Data.SQLite
) as my app's db for a long time, and everything was ok. Recently i start getting a 'Database is locked'. My DAL has few functions, each of them reads and writes, and uses a newly created connection within a using
block. When i run my tests, the error occures at the second call to one of those functions. But when i debug and move line after line, it's ok. My app is single threaded.
Someone has a idea?
Many thanks.
Solução
Outras dicas
Are you running the SQLite console or SqliteAnalylitics exes at the same time?
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow