3

I have an R Plumber API and it's purpose is to receive webhook data, sanitise the data, and then pass it on to a relational database.

Is it considered best practice for the API to maintain a constant state of connection to the DB (something like con <- DBI::dbConnect(...)) and just never closing the connection, or is it instead best practice to open and close the connection between the Plumber API and the DB each time data is sent?

I realise this is somewhat subjective, but I do urge any potential 'closers' to be mindful of how helpful this discussion can be for a developer who is not a database expert. Thank you in advance.

Davide Lorino
  • 875
  • 1
  • 9
  • 27
  • 4
    I've found that maintaining a constant connection requires that any DB-reaching code using the connection always has to check if it has gone invalid for some reason (network glitch?) and reconnect if needed. While not impossible, it does complicate code unnecessarily. One fix for this is to use the `pool` package, which is typically pretty good at internally handling this eventuality. Another fix is to connect, query, disconnect for each call ... which might be more than you want. – r2evans Apr 12 '21 at 01:13

0 Answers0