'RSQLite why and when to use disconnect

I'm building a shiny app that connects to a SQLite database. It enables the user to see tables and update and insert data.

Now my question is whether it is better to connect once at the start of the app and disconnect once when closing it. Or everytime a query is executed (connect -> execute query -> disconnect).

Approach 1:

  1. App starts -> dbConnect() (only once)
  2. User does stuff. Insert, read, update...
  3. App closes -> dbDisconnect() (only once)

Approach 2:

  1. App starts -> dbConnect(), read tables, dbDisconnect()

  2. User inserts or updates data -> dbConnect(), execute SQL-Query, dbDisconnect()

  3. Open DB, execute Query, Close DB repeats for every interaction with DB...

What are the benefits of each approach? Is the connection blocked for the time the connection is held open? And what how is opening and closing the DB affecting the performance?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source