Forum Cross Platform with Xamarin

SQLite.Net - Connection pooling, locks, etc


When dealing with SqlLite.Net, what is your strategy when it comes to handle the connections in order to prevent contention?

I have seen code samples out there (including in the SqlLite.Net component help page) like this:

using(var connection = new SQLiteConnection (DbPath)){ //do something with it }

Which off course creates the connection and releases it after manipulating the db.
Do you use this style?

On the other hand, I have seen other examples (Tasky) where they keep the connection alive and pass the handler around when needed.
Is this a good practice?

Should I care only for not opening multiple connections to the same *.db file or even when I share the connection, should I also lock all the operations in order to prevent contention?



  • We keep a single connection alive and use it, in addition we have implicit locking through the use of a custom taskscheduler which runs all tasks on a single thread (We use the AsyncConnection part of SQLite.Net).

  • rrodriguezrrodriguez USMember

    Hi @OysteinKrog,

    I'm currently evaluating CouchBase since we use a document db on the server too and it feels like a better fit for our applicaition.
    What I decided to do before considering moving to CouchBase was isolate our *.db files per service, and keep a single/reusable connection per .db/service; being each service a singleton.

    This was working fine on the few tests I did; but I haven't tested extensively.

Sign In or Register to comment.