通用資料庫介面 sql.DB

GORM 提供方法 DB,可從目前的 *gorm.DB 傳回通用資料庫介面 *sql.DB

// Get generic database object sql.DB to use its functions
sqlDB, err := db.DB()

// Ping
sqlDB.Ping()

// Close
sqlDB.Close()

// Returns database statistics
sqlDB.Stats()

注意 如果底層資料庫連線不是 *sql.DB,例如在交易中,它會傳回錯誤

連線池

// Get generic database object sql.DB to use its functions
sqlDB, err := db.DB()

// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
sqlDB.SetMaxIdleConns(10)

// SetMaxOpenConns sets the maximum number of open connections to the database.
sqlDB.SetMaxOpenConns(100)

// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
sqlDB.SetConnMaxLifetime(time.Hour)

白金贊助商

黃金贊助商

白金贊助商

黃金贊助商