Keep sqlite database connection open while using WAL/Multiuser?

Right now my app opens and closes the database in every subroutine that uses it.

This is a multi process app that uses WAL/MultiUser.

Can I just open the connection once and keep it open? It would really clean up my code…

What do you mean by “multi process app”?

In general, if there is only a single user/app and the database is local, then it is common practice to open it once when the app starts and close it when the app ends.

If you are threading access to the DB then you might want to open once for each thread and close when the thread ends.

Unless your app is a web app, in which case you’ll want one connection per Session.

In addition to Paul and Greg replies you can consider other scenario:

If your SQLite database is on a remote / network device and you want to use it as a Client/Server database, with your app acting as a client (for example multiple users using the app against the SQLite DB). then it is highly recommended that you use some third-party solution, as for example CubeSQL or Valentina.

Here’s the scenario: One process is a web app and the other process looks into the same table to fire off jobs. Can I keep the DB open in both apps? Both apps are on the same machine along with the database.

That should be no problem, but as I said, if there is more than one user in that web app you’ll need to put a database property on the Session class and have a separate one for each user.

Yes - each process can safely open the DB

You guys are great! Thanks everyone!