Does the sqlite-net async API support dispose?
Asked Answered
P

2

12

I'm using the sqlite-net async API to code a Windows Phone app. While writing a unit test to make sure that my adapter to the sqlite-net API created the file properly, I noticed that the adapter continues holding onto the file handle even after it has gone out of scope.

The async connection class (SQLiteAsyncConnection) does not support IDisposable so I cannot manually dispose it. Looking at the source, it seems like the async API creates a connection, uses it and disposes it every time. However, when my test cleanup code attempts to delete the created test database, some other resource is still holding onto it.

Properly answered 25/1, 2014 at 7:15 Comment(2)
If that was the case perhaps you just need to call GC.Collect() before trying to delete. However, I'm not seeing this behavior myself. :-(Vulcan
More than ten years ago they already knew NOT to call GC.Collect() manually blogs.msdn.com/b/ricom/archive/2003/12/02/… :)Confederation
C
4

I had the exact same problem in case where database was supposed to be sent via email for debugging/analyzing purposes. There was no way of doing that because connection is held.

You can go around this by modifying the sqlite-net implementation a bit.

On top of SQLiteAsync.cs add following partial class declaration.

namespace SQLite
{
    public partial class SQLiteAsyncConnection
    {
        public void ResetConnections()
        {
            SQLiteConnectionPool.Shared.Reset();
        }
    }
}

and then change access modifier of above Reset method as public.

In your application code just call <YourSQLiteAsyncConnection>.ResetConnections(); before deleting the DB file.


Then again, when testing you could create your database in memory so you don't have to delete it at all. You can achieve this by setting DatabasePath as ":memory:".

Confederation answered 5/8, 2015 at 17:10 Comment(0)
C
2

On November 23rd 2015 they added a static method that does what Mikko Viitala suggested, So I just put the following in my dispose method.

SQLiteAsyncConnection.ResetPool();
Cubiform answered 14/2, 2017 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.