Your Question

How do we configure a connection timeout while connecting to the database?

Context: If the database is not responding or is in a hung state, and if we try to establish a connection to the database, the entire code is hung indefinitely because there is no way of specifying a connection timeout.

The document you expected this should be explained

Connect to Database

Expected answer

We can specify the Connection Timeout as part of the config being passed to Gorm to establish the connection.

db, dbConnectionError = gorm.Open(postgres.Open(dsn), &gorm.Config{SkipDefaultTransaction: false, ConnectitonTimeout: 10 * time.Second}) 

Comment From: nithinb

@jinzhu Did you get a chance to look at this request?

Comment From: xing393939

i think we can use the dsn parameters, postgres: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-CONNECT-TIMEOUT mysql: https://github.com/go-sql-driver/mysql#timeout