r-dbi / DBI

A database interface (DBI) definition for communication between R and RDBMSs

Home Page:https://dbi.r-dbi.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Document keepalive for connections using the pool package [Lost connection to MySQL server during query [2013]]

vituri opened this issue · comments

I am constantly getting the error Lost connection to MySQL server during query [2013], which seems to be related to the connection timeout. This happens when I create a con object and try to use it to collect a table after some time (say, 10 minutes). My connection is created using the command

con = 
RMariaDB::dbConnect(
      drv = RMariaDB::MariaDB(),
      username = username,
      password = password,
      host = host,
      port = port,
      dbname = dbname,
      timeout = -1
    )

I also tried to put "timeout = Inf" but with no success. Is the "timeout" argument really working? I am using the github version of DBI and RMariaDB packages.

Thanks. Is a connection timeout (=dropping of existing inactive connections) configured in the server?

Here are some of the variables in the server:

wait_timeout: 28800
connect_timeout: 10
idle_transaction_timeout: 0

I don't remember having this error before, and I didn't change the MariaDB (version 10.4.8) configurations since I start using it 8 months ago. What do you suggest?

Thanks, the timeouts look good to me. There's also interactive_timeout but it's only used with connections that set the CLIENT_INTERACTIVE flag.

Do the server logs give a hint on what's happening here?

I couldn't get the logs, but found a kind of solution using the "pool" package. It takes care of the connections (in case of several users connecting in a shinyapp) and keep sending small queries so the connection doesn't turn off. I just had to change the connector to

con =
    pool::dbPool(
      drv = RMariaDB::MariaDB(),
      username = username,
      password = password,
      host = host,
      port = port,
      dbname = dbname,
      validationInterval = 300
    )

This is a very neat trick, thanks for sharing!

Need to find a place where to document this trick.