在我的golang应用程序中,与Postgres数据库连接数次后,出现错误: pq:对不起,已经有太多客户端了
但是当调用的函数完成时,我将关闭语句。知道为什么会发生吗?
ctx := context.Background()
var err error
if db == nil {
err = errors.New("db is null")
log.Panic("db is null")
return err
}
// Check if database is alive.
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
if err := db.PingContext(ctx); err != nil {
log.Fatal(err)
}
tsql := `INSERT INTO jst_pac_primary_activity
(pac_text,
pac_code,
pac_pre_establishment_id)
VALUES
($1,
$2,
$3);`
stmt, errStmt := db.Prepare(tsql)
if errStmt != nil {
log.Fatal("Error to prepare query")
}
defer stmt.Close()
stmt.QueryRow(EstablsihmentData.AtividadePrincipal[index].Text,
EstablsihmentData.AtividadePrincipal[index].Code,
establishmentId)