kubernetes - Goroutine 在每个请求(sqlx)和代码后打开一个新的数据库连接
问题描述
让我们考虑以下 goroutine:
func main(){
...
go dbGoRoutine()
...
}
和功能:
func dbGoRoutine() {
db, err := sqlx.Connect("postgres", GetPSQLInfo())
if err != nil {
panic(err)
}
defer db.Close()
ticker := time.NewTicker(10 * time.Second)
for _ = range ticker.C {
_, err := db.Queryx("SELECT * FROM table")
if err != nil {
// handle
}
}
}
每次函数在股票代码上迭代时,它都会打开一个 cloudSQL 连接
[service... cloudsql-proxy] 2019/11/08 17:05:05 New connection for "location:exemple-db"
我不知道为什么每次都会打开一个新连接,因为 sqlx.Connect 不在 for 循环中。
解决方案
推荐阅读
- node.js - NODE 和 SQL Server 事务实现
- node.js - 如何满足以下 Nodejs 单元测试中的测试覆盖率?
- html - HTML/CSS 和 svg
- python - 仅连续日期的累计金额
- python-3.x - ModuleNotFoundError:没有名为“pony.orm.dbproviders”的模块
- javascript - 如何在事件中使用反应钩子
- curl - 如何从 Apache Solr 中的 attr_text 中删除所有“\n”?
- algorithm - 可视化 4D 转置
- sql - 消除列中的每个重复值
- html - 在css中创建具有特定形状的对话气泡时,在后面设置透明度