Mage Xy
Mage Xy

Reputation: 1803

Should I open a single or multiple connections to a local SQLite database?

I am in the process of writing a Xamarin.Forms app. I have a database manager class which handles inserting, deleting, etc. from a local SQLite database. Since the DB file is locally stored on a single device, only one user will ever be accessing the DB at any given time. I'm not reading/writing to the DB often, but when I do, I'm grabbing a lot of data.

My question is this: Should I create a single connection to the DB and simply reuse that over and over throughout the entire application, or is it better to create a new connection every time I access the DB? Creating a new connection each time would probably be more flexible (for my app anyway), but I'm worried about the overhead.

Upvotes: 2

Views: 2567

Answers (1)

Adam
Adam

Reputation: 16199

If you are going to use a lot of queries straight after each other to pull data from the DB, then I would reuse the SQLite connection. There is overhead for each connection request and nothing is really gained by creating a new one each time, other than just more work.

I accidentally had my application creating new connections on each query. I noticed an increase in speed when I corrected it back to reusing a single connection.

Upvotes: 4

Related Questions