[Go] golang实现mysql连接池
时间:2021-07-01 10:21:17
帮助过:10人阅读
"database/sql"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, _ := sql.Open(
"mysql",
"root:123456@tcp(127.0.0.1:3306)/gocron")
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(5)
//连接数据库查询
for i :=
0; i <
100; i++
{
go func(i int) {
mSql :=
"select * from user"
rows, _ :=
db.Query(mSql)
rows.Close() //这里如果不释放连接到池里,执行5次后其他并发就会阻塞
fmt.Println(
"第 ", i)
}(i)
}
for {
time.Sleep(
time.Second)
}
}
开一个窗口不停的netstat
while true;do clear;date;netstat -altupn|grep 3306|grep Client;sleep 1;done
开一个窗口tcpdump看闲置连接的请求情况,每隔15秒请求一次数据
tcpdump -i lo port 3306 -vv
[Go] golang实现mysql连接池
标签:Golan UNC int driver src idle drive 对象 done