-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Closed
Labels
Description
my test code is this:
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"runtime"
"strconv"
"time"
)
var num chan int
var allLogin int
func dbInsert(loginInsert *sql.Stmt, i int) {
_, err := loginInsert.Exec("zc"+strconv.Itoa(i), "123456")
if err != nil {
fmt.Println(strconv.Itoa(i)+"行:", err)
}
num <- 1
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
db, err := sql.Open("mysql", "root:198771zc@tcp(localhost:3306)/mygame?charset=utf8")
if err != nil {
fmt.Println(err)
return
}
num := make(chan int)
//db.SetMaxIdleConns(runtime.NumCPU())
loginInsert, err := db.Prepare("insert into loginer values(null, ?, ?)")
fmt.Println(time.Now())
for i := 0; i < 1000; i++ {
go dbInsert(loginInsert, i)
}
for {
select {
case <-num:
allLogin++
}
if allLogin == 1000 {
fmt.Println(time.Now())
break
}
}
time.Sleep(time.Minute)
}
I used dbPrepare and .Exec
I read the guide it says the *Stmt is thread-safe
So can you help me to solve the problem,how can I safe use the db int goruntine?