System.Data.SQLite

View Ticket
Login
Ticket Hash: 1ef72fac07013eb6654d5865764f08d21ba297ec
Title: EF6 keeps lock on a database file after context disposed
Status: Closed Type: Incident
Severity: Important Priority: Medium
Subsystem: LINQ Resolution: Rejected
Last Modified: 2016-08-25 00:34:44
8.86 years ago
Created: 2015-08-22 17:23:43
9.87 years ago
Version Found In: 1.0.98.1
User Comments:
anonymous added on 2015-08-22 17:23:43:
When reading data from an SQLite database file using Entity Framework 6, the file keeps locked even after the context is disposed. I use the latest System.Data.SQLite package from Nuget.

using (var conn = new SQLiteConnection(@"Data Source=test.db3"))
using (var context = new DatabaseContext(conn))
{
	var value = context.TestTable.First().TestField;
	Console.WriteLine(value);
}
File.Move(@"test.db3", @"test2.db3"); // System.IOException occurs there - the file is locked.

VS2015 solution that shows the problem:
[https://dl.dropboxusercontent.com/u/42211465/SQLiteEF6FileIssue.zip]

mistachkin added on 2015-08-22 19:12:30:
This is most likely the same as an issue we saw before.  The Entity Framework
does not dispose of all IDisposable resources, which prevents them (and their
associated connection) from being cleaned up, hence the lock.

anonymous (claiming to be Eric Lloyd) added on 2016-08-16 21:12:56:
In my experience, closing the database connection directly doesn't solve the problem either. According to this StackOverflow answer [http://stackoverflow.com/a/8513453/136062], System.Data.SQLite isn't actually releasing its hold on the file until a garbage-collection cycle finalizes the connection.

My code has to issue a GC.Collect() and GC.WaitForPendingFinalizers() to successfully release SQLite's locks on the database.

mistachkin added on 2016-08-25 00:34:44:
It's the Entity Framework that is not disposing of all its IDisposable database
resources that is the root cause of this problem.  Case closed.