If I create a non-async Connection, there is a Close method I can access. However, there isn't a Close method for async connections. Should there be?
I've found the solution to this, and this needs to be better documented because the implication in the source is that this needs to be done when the app is suspending.
The solution is to call SQLiteConnectionPool.Shared.Reset(); This closes all of the open connections.
If I've got this wrong, perhaps the code maintainers can provide the correct solution.
Thanks,
SQLiteConnectionPool is not public so it cannot be called!
You can use reflection to call it. In my attempts it worked perfectly for Android and Windows, it fires back after 3x or 4x such closed connections on iOS. Damn... I was almost tempted to go for this solution but on iOS (simulator) it does not seem to be a viable solution.
There must be a reason the creators did the Reset method internal so all in all this approach is probably not recommended but I need to close the connection time to time... :-(
private static void CloseDatabaseConnectionViaReflection()
{
var assembly = typeof(SQLiteAsyncConnection).GetTypeInfo().Assembly;
var sqLiteConnectionPoolType = assembly.GetTypes().First(t => t.Name == "SQLiteConnectionPool");
var resetMethod = sqLiteConnectionPoolType.GetMethod("Reset");
var sqLiteConnectionPoolSharedInstance =
sqLiteConnectionPoolType
.GetFields(BindingFlags.Static)
.First(field => field.IsInitOnly && field.Name == "_shared")
.GetValue(null);
resetMethod.Invoke(sqLiteConnectionPoolSharedInstance, null);
}
Pls has this issue been resolved? I also need the close() method on the async connection.
Up for the CloseAsync() method, and generally the missing async counterparts in the SQLiteAsyncConnection.
Why doesn't it expose an async version for all the methods provided by SQLiteConnection?
How am I supposed to commit and close the connection when using the async way?
Via SQLiteAsyncConnection.GetConnection()?
Maybe @praeclarum can easily clarify this aspect.
Yeah, this is all my fault. I still use the synchronous version so the Async version gets neglected.
I've created Issue #480 to remind me to get these back in parity.
This issue will track the change to expose SQLiteConnectionPool.Shared.Reset
The solution is to call SQLiteConnectionPool.Shared.Reset(); This closes all of the open connections.
This issue will track the change to expose SQLiteConnectionPool.Shared.Reset
Isn't that dangerous? If you want to just close your current connection, exposing a CloseAsync method on the SQLiteAsyncConnection would be sufficient. You don't always know what other async or parallel process might have connections still being used before you just force shut them all down. I'm not sure that the pool reset needs to be exposed if CloseAsync is provided.
Working on a CloseAsync method now.
Thank you so much!
To cleare that pool call:
SQLiteAsyncConnection.ResetPool();
im using sqlite-net-pcl 1.4.118
_connection.GetConnection().Close();
_connection.GetConnection().Dispose();
SQLiteAsyncConnection.ResetPool();
now you can establish a new connection
Most helpful comment
Working on a
CloseAsyncmethod now.