Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GORM不执行查询 #6258

Closed
LeftH0ok opened this issue Apr 22, 2023 · 6 comments
Closed

GORM不执行查询 #6258

LeftH0ok opened this issue Apr 22, 2023 · 6 comments
Assignees
Labels

Comments

@LeftH0ok
Copy link

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}



@LeftH0ok LeftH0ok added the type:question general questions label Apr 22, 2023
@github-actions github-actions bot added type:missing reproduction steps missing reproduction steps and removed type:question general questions labels Apr 22, 2023
@LeftH0ok LeftH0ok changed the title GORM不执行增删改查 GORM不执行查询 Apr 22, 2023
@LeftH0ok
Copy link
Author

服务器是阿里云MySQL5.6数据库

@github-actions
Copy link

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.ioSearch Before Asking

@labulakalia
Copy link
Contributor

写具体点,哪句SQL不执行查询

@github-actions
Copy link

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.ioSearch Before Asking

@a631807682
Copy link
Member

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?

@github-actions
Copy link

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.ioSearch Before Asking

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants