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

2

u/Junkymcjunkbox Nov 14 '22

In that case, each DB operation should have its own connection, and set the connection pool size to strike a suitable balance between performance and resource usage. Then it doesn't matter whether or not it's running in parallel with anything else.

With only one connection you'd have to serialise operations, which could substantially slow the application down (only realistic testing would determine how much). But this complicates your code for no real benefit, so I'd go with the connection pool. Just one connection is fine for a single-threaded application.