GORM provides the method DB which returns a generic database interface *sql.DB from the current *gorm.DB

  1. // Get generic database object sql.DB to use its functions
  2. sqlDB, err := db.DB()
  3. // Ping
  4. sqlDB.Ping()
  5. // Close
  6. sqlDB.Close()
  7. // Returns database statistics
  8. sqlDB.Stats()

NOTE If the underlying database connection is not a *sql.DB, like in a transaction, it will returns error

Connection Pool

  1. // Get generic database object sql.DB to use its functions
  2. sqlDB, err := db.DB()
  3. // SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
  4. sqlDB.SetMaxIdleConns(10)
  5. // SetMaxOpenConns sets the maximum number of open connections to the database.
  6. sqlDB.SetMaxOpenConns(100)
  7. // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
  8. sqlDB.SetConnMaxLifetime(time.Hour)