db.Ping和db.Stats()以及数据库连接的单例模式结果如下
func GetInstance() *gorm.DB {
host := viper.GetString("mysql.default.hostname")
user := viper.GetString("mysql.default.username")
port := viper.GetString("mysql.default.port")
pass := viper.GetString("mysql.default.password")
dbname := viper.GetString("mysql.default.database")
prefix := viper.GetString("mysql.default.prefix")
once.Do(func() {
if db == nil {
var err error
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4", user, pass, host, port, dbname)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: prefix, // table name prefix, table for `User` would be `t_users`
SingularTable: true, // use singular table name, table for `User` would be `user` with this option enabled
//NameReplacer: strings.NewReplacer("CID", "Cid"), // use name replacer to change struct/field name before convert it to db name
},
//Logger: logger.Default.LogMode(logger.Info),
Logger: logger.New(Writer{}, logger.Config{
SlowThreshold: 0,
//Colorful: true,
IgnoreRecordNotFoundError: false,
LogLevel: logger.Info,
}),
})
if err != nil {
fmt.Println(dsn)
panic(err)
}
}
})
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(10)
sqlDB.SetMaxOpenConns(100)
return db
}
func Orm(ctx *gin.Context) {
orm := gormx.GetInstance()
d, _ := orm.DB()
stats := d.Stats()
err := d.Ping()
log.Printf("ping err: %#v, stats: %#v", err, stats)
}
2023/04/22 20:15:35 ping err: <nil>, stats: sql.DBStats{MaxOpenConnections:100, OpenConnections:2, InUse:1, Idle:1, WaitCount:0, WaitDuration:0, MaxIdleClosed:0, MaxIdleTimeClosed:0, MaxLifetimeClosed:0}
2023/04/22 20:15:35 ping err: <nil>, stats: sql.DBStats{MaxOpenConnections:100, OpenConnections:2, InUse:1, Idle:1, WaitCount:0, WaitDuration:0, MaxIdleClosed:0, MaxIdleTimeClosed:0, MaxLifetimeClosed:0}
2023/04/22 20:16:38 ping err: <nil>, stats: sql.DBStats{MaxOpenConnections:100, OpenConnections:2, InUse:1, Idle:1, WaitCount:0, WaitDuration:0, MaxIdleClosed:0, MaxIdleTimeClosed:0, MaxLifetimeClosed:0}
2023/04/22 20:16:41 ping err: <nil>, stats: sql.DBStats{MaxOpenConnections:100, OpenConnections:2, InUse:1, Idle:1, WaitCount:0, WaitDuration:0, MaxIdleClosed:0, MaxIdleTimeClosed:0, MaxLifetimeClosed:0}
2023/04/22 20:16:41 ping err: <nil>, stats: sql.DBStats{MaxOpenConnections:100, OpenConnections:2, InUse:1, Idle:1, WaitCount:0, WaitDuration:0, MaxIdleClosed:0, MaxIdleTimeClosed:0, MaxLifetimeClosed:0}
2023/04/22 20:16:42 ping err: <nil>, stats: sql.DBStats{MaxOpenConnections:100, OpenConnections:2, InUse:1, Idle:1, WaitCount:0, WaitDuration:0, MaxIdleClosed:0, MaxIdleTimeClosed:0, MaxLifetimeClosed:0}
2023/04/22 20:16:42 ping err: <nil>, stats: sql.DBStats{MaxOpenConnections:100, OpenConnections:2, InUse:1, Idle:1, WaitCount:0, WaitDuration:0, MaxIdleClosed:0, MaxIdleTimeClosed:0, MaxLifetimeClosed:0}
2023/04/22 20:16:43 ping err: <nil>, stats: sql.DBStats{MaxOpenConnections:100, OpenConnections:2, InUse:1, Idle:1, WaitCount:0, WaitDuration:0, MaxIdleClosed:0, MaxIdleTimeClosed:0, MaxLifetimeClosed:0}
2023/04/22 20:16:43 ping err: <nil>, stats: sql.DBStats{MaxOpenConnections:100, OpenConnections:2, InUse:1, Idle:1, WaitCount:0, WaitDuration:0, MaxIdleClosed:0, MaxIdleTimeClosed:0, MaxLifetimeClosed:0}
Comment From: LeftH0ok
服务器是阿里云MySQL5.6数据库
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: labulakalia
写具体点,哪句SQL不执行查询
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: a631807682
You need to provide reproducible or observable code, no one can know the specific problem from your description. Is it about deadlock? Or about freeze? Or read and write on conn?
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨