Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the sqlite-net async API support dispose?

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.

like image 590
sohum Avatar asked Jan 25 '14 07:01

sohum


2 Answers

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();
like image 187
numerek Avatar answered Oct 05 '22 23:10

numerek


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:".

like image 45
Mikko Viitala Avatar answered Oct 05 '22 23:10

Mikko Viitala