一直是这个问题困扰着哦我,为什么越来越多连接,到爆掉,换作postgresql驱动也是,还是我理解go语言并发有错呢
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"strconv"
"time"
)
var xiancheng = 10
var over = make(chan bool)
var sem = make(chan int, xiancheng)
func checkErr(err error, hh int) {
if err != nil {
fmt.Printf("error "+strconv.Itoa(hh)+"\n", err)
panic(err)
}
}
func getMySQL2() *sql.DB {
db, err := sql.Open("mysql", "root:123456789@tcp(127.0.0.1:3306)/getbbs?charset=utf8")
checkErr(err, 999)
return db
}
func work(aa int) string {
sem <- 1
dbx := getMySQL2()
defer dbx.Close()
_, e2 := dbx.Query("select 1")
if e2 == nil {
println("DB OK:", aa)
}
time.Sleep(time.Second * 1)
fmt.Println("work id:", aa)
if aa == 9999 {
gocheck2()
}
<-sem
return "ok"
}
func gocheck2() {
over <- true
}
func main() {
for i := 0; i < 10000; i++ {
go work(i)
}
<-over
}
请这里的高手帮帮忙
有疑问加站长微信联系(非本文作者)