2

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.

Mage Xy
  • 1,803
  • 30
  • 36
  • If you are worried about performance, you should measure the difference between the two approach. You are the only one that has the file, know what data retrieve and has already the code in place. – Steve Nov 18 '15 at 20:02

1 Answers1

3

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.

Adam
  • 16,089
  • 6
  • 66
  • 109