package main
import "fmt"
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
import "time"
func main() {
ch := make(chan int, 10)
go searchsql(ch)
go searchsql(ch)
go searchsql(ch)
time.Sleep(1 * time.Second)
}
func searchsql(p <-chan int) {
var id int
var account string
db, err := sql.Open("mysql", "root:password@tcp(192.168.0.31:3306)/app?charset=utf8")
if err != nil {
fmt.Println("failed to open database:", err.Error())
return
}
tx, _ := db.Begin()
rows, err := tx.Query("select id,account from accounts where flag=0 limit 1 for update")
if err != nil {
fmt.Println("fetech data failed:", err.Error())
return
}
defer rows.Close()
for rows.Next() {
rows.Scan(&id, &account)
fmt.Println("id:", id, "name:", account)
}
tx.Exec("update accounts set flag=? where id=?", "1", id)
tx.Commit()
defer db.Close()
}