由于我没有在 Close() 函数中找到 *gorm 实例,任何帮助将不胜感激
dbURI := fmt.Sprintf("user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s",
"username", "password", "dbname", "5432", "disable", "Asia/Kolkata")
fmt.Println(dbURI)
connection, err := gorm.Open(postgres.Open(dbURI), &gorm.Config{})
if err != nil {
fmt.Println("Error connecting database")
panic(err.Error())
} else {
fmt.Println("Connected to database")
}
注意:connection.Close() 不适用于 GORM 1.20.0
金珠决定消灭Close()
1.20 版本上的方法,因为 GORM 支持连接池,所以正确的用法是打开一个连接并在应用程序中共享它。
如果您的特定用例仍然需要使用Close()
方法,GORM提供了方法DB,返回一个数据库通用接口 https://gorm.io/docs/generic_interface.html你可以在哪里使用它。
举个例子
sqlDB, err := db.DB()
// Close
sqlDB.Close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)