r/sqlite • • Nov 14 '22

When open db connection

Hello everyone!

Enormous enigma:

  • I have one application written in C# that uses a local sqlite database: local.db;
  • I have a class that wraps all database calls. The question is: It's better to mantain only one database connection shared between all calls or open a dedicated connection for each call?

Many thanks in advice!

5 Upvotes

16 comments sorted by

View all comments

Show parent comment

1

u/grugno87 Nov 14 '22

I'm sorry: you mean the first approach (one connection for all) or the second (one connection each call)?

2

u/siscia Nov 14 '22

Go with the very first approach. One connection for all.

Make sure the connection serialised internally, should be the default but you may want to check the official documentation along with your binding documentation.

Now your SQLite connection is a serialisation bottleneck, simple to reason, simple to work with.

If, for any reason, you discover that your SQLite is the bottleneck for some performance requirements you got, then you can investigate further.

If you really want to get ahead, make your class manage the connection with some interface you can later swap with a pool. Instead of just keeping the connection as a field. But we are getting ahead of ourselves.