汎用データベースインターフェース sql.DB

GORMは、現在の*gorm.DBから汎用データベースインターフェース*sql.DBを返す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)

プラチナスポンサー

ゴールドスポンサー

プラチナスポンサー

ゴールドスポンサー