1 回答
TA贡献1856条经验 获得超5个赞
Close您的引导代码中有一个数据库连接调用:
func DbClient(cfg *config.Config, logger log.Logger) *ent.Client {
// "host=<host> port=<port> user=<user> dbname=<database> password=<pass>"
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s",
cfg.DB_HOST, cfg.DB_PORT, cfg.DB_USER, cfg.DB_NAME, cfg.DB_PASS)
client, err := ent.Open("postgres", psqlInfo)
if err != nil {
logger.Fatal(err)
}
defer client.Close() // <- this one
logger.Info("Database Connected")
if err := client.Schema.Create(context.Background()); !errors.Is(err, nil) {
logger.Fatalf("Error: failed creating schema resources %v\n", err)
}
return client
}
返回后此调用关闭连接client。如果需要关闭连接,请从CreateUser()func 中将其关闭。
func CreateUser(client *ent.Client, input *model.NewUser) (*ent.User, error) {
user, err := client.Debug().User.Create().SetName(input.Name).SetUsername(input.Username).SetEmail(input.Email).SetPassword(input.Password).Save(context.Background())
if err != nil {
return &ent.User{}, err
}
defer client.Close() // <- here
return user, nil
}
- 1 回答
- 0 关注
- 144 浏览
添加回答
举报