"Nested" trycatch()


I am using trycatch when attempting to connect to multiple databases to sync tables.
All databases have the same schema, but are on different servers and the sync is sequential. Connect to A, sync tables, disconnect, connect to B... using the same functions.

Trycatch allows me to log info about success of establishing the connection & to skip one database if error is raised and continue with subsequent database. Simple example:


con <- function(dbname, host, username, password){ #passing parameters for the connection
    expr = {
      con = dbPool(RMariaDB::MariaDB(), dbname = dbname, host = host, username  = username, password = password)
      log_info(paste("Successfully logged to database", dbname))
      return(con) #return connection object
    error = function(e){
      log_fatal(paste('Error connecting to database', dbname))    

I would like to add an option to try to establish the connection multiple times if unsuccessful at first (error connecting to database) & log all unsuccessful attempts. For example log_error(paste('Error connecting to database', dbname)) -> Sys.sleep(60) -> attempt to connect to db again. If unsuccessful for 5 times, log_fatal(paste('Fatal error connecting to database', dbname)) and continue with the next database.


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.