Is there a way to reestablish SQL Server connection with R Shiny app?

My Shiny app keeps getting disconnected from the server.
This is the error I keep getting on the Posit logs:

Warning: Error in eval: nanodbc/nanodbc.cpp:4616: 00000: [Microsoft][ODBC Driver 17 for SQL Server]Communication link failure

My app works just fine locally. I really don’t think this is caused by code in my app, but if it is, then what kind of coding mistake would cause this?

Also to give full context whenever I update my app in Posit Connect it actually works for about an hour and then crashes. As a temporary solution I keep adding in comments in my code and pushing to GitLab as an excuse to update the app.

4

Here is my way used for many years in my projects to reestablish SQL Server global connection with R Shiny app.


library(shiny)
...
library(DBI)
library(odbc)

database_encoding =  "latin1"  

shinyServer(function(input, output, session) {

    react <- reactiveValues(
       my_connection_db1= 0
    )

  connection_react <-   function () {
    
    # react$my_connection_db1 is set at the beginning to 0 instead of NULL, 
    # for next connections, to know if there was an initial connection

    if (!is.null(react$my_connection_db1)) {
      
      
      tryCatch(
        {
          DBI::dbGetInfo(react$my_connection_db1)
        },
        error=function(e) {    
          print("Lost of connection my_connection_db1, try to reconnect (Error)")
          react$my_connection_db1<- tryCatch(
                                       DBI::dbConnect(odbc::odbc(),
                                        .connection_string=my_connection_db1_odbc_string, 
                                         encoding = database_encoding
                                       )
                                       , error=function(e) { NULL}
                                    )
        },
        warning=function(w) {  
         print("Lost of my_connection_db1 (Warning)")
        }
      )
      
    }
    return(react$my_connection_db1)
  }
...
}

Used everywhere in reactive components

Mydata <- reactive({
  DBI::dbGetQuery(
    connection_react(), 
    iconv(
      paste0("SELECT * FROM TOTO ... WHERE ..."
             "
      ),"UTF-8","latin1"
    )
  )
})

My initial need was for switch between 2 database (archive, actual) with a similar implementation but with 2 connections.

I’m using a similar code snippet as @phili_b to ensure I’m always using a valid Postgres connection.

Note that for the Postgres driver, DBI::dbGetInfo always returns data even if the connection is closed by the server.

A workaround is to use a function that effectively requests data from the server. DBI::dbListTables is a good candidate as the result does not need to be fetched.

# Initialize first database connection
database_keepalive <- reactiveValues(
    link = tryCatch(
               dbConnect(RPostgres::Postgres(),
                         "postgres",
                         "localhost",
                         5432,
                         "postgres",
                         ""),
               error=function(e){
                    print("Error connecting to the database. Are you sure your Postgres database is running ? That your credentials are correct ?")
                    print("Full Stack Trace")
                    print(e)
                    NULL
               }
          )
)

# Function to call to ensure to always receive a valid database connection
session_db <- function() {
    tryCatch({
        DBI::dbListTables(database_keepalive$link)
     },
     error=function(e) {
          print("Lost of connection database link, reconnecting...")
          database_keepalive$link <- tryCatch(
               dbConnect(RPostgres::Postgres(),
                         "postgres",
                         "localhost",
                         5432,
                         "postgres",
                         ""),
               error=function(e){
                    print("Error connecting to the database. Are you sure your Postgres database is running ? That your credentials are correct ?")
                    print("Full Stack Trace")
                    print(e)
                    NULL
               }
          )
        
    })
    return(database_keepalive$link)
}

# PostgreSQL Connection with keep alive usage
dbSendQuery(session_db(), sql("SELECT 1")

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật