GORM Playground Link

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)

}

https://go.dev/play/p/-MKUWeDBml7

Description

可以ping以及观测stats状态,但是不执行sql

2023/04/22 20:15:35 ping err: , 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: , 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: , 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: , 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: , 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: , 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: , stats: sql.DBStats{MaxOpenConnections:100, OpenConnections:2, InUse:1, Idle:1, WaitCount:0, WaitDuration:0, MaxIdleClosed:0, MaxIdleTimeClosed:0, MaxLifetimeClosed:0}

Comment From: LeftH0ok

@jinzhu

Comment From: LeftH0ok

@sunfmin

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

duplicated of https://github.com/go-gorm/gorm/issues/6258