Skip to content

关于client多协程问题 #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
always-waiting opened this issue Dec 16, 2019 · 5 comments
Closed

关于client多协程问题 #456

always-waiting opened this issue Dec 16, 2019 · 5 comments

Comments

@always-waiting
Copy link
Contributor

下面是一个示例

package main

import (
	"fmt"
	"github.com/siddontang/go-mysql/client"
	"sync"
)

const (
	SQL1 = "select device_sn from t_device_basic where id = ? and is_delete = 0"
)

func main() {
	fmt.Println("vim-go")
	cli, err := client.Connect(
		"localhost:3306",
		"root",
		"abc123",
		"db_cmdb",
	)
	if err != nil {
		panic(err)
	}
	fmt.Println("test")
	var wg sync.WaitGroup
	for _, id := range []int{1, 2, 3, 4, 5} {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			res, err := cli.Execute(SQL1, i)
			if err != nil {
				fmt.Printf("%d查询出错:%v\n", i, err)
				return
			}
			num := res.RowNumber()
			for n := 0; n < num; n++ {
				var get string
				get, err := res.GetStringByName(n, "device_sn")
				if err != nil {
					fmt.Printf("获取第%d的sn出错", n)
				}
				fmt.Println(get)
			}
		}(id)
	}
	wg.Wait()
}

如上,希望可以只用一个mysql链接,给多个协程进行处理,这时会出如下错误:

vim-go
test
5查询出错:invalid sequence 1 != 3
2查询出错:invalid sequence 0 != 3
4查询出错:invalid sequence 1 != 3
1查询出错:invalid sequence 0 != 3
3查询出错:invalid sequence 2 != 1

除了加锁调用,是否还能有其他解决方案呢?

@always-waiting
Copy link
Contributor Author

@siddontang
这个问题,能够从包层面进行优化吗?

@siddontang
Copy link
Collaborator

一个 conn 只能用在一个 goroutine,不支持并发访问

@meshenka
Copy link

can you translate your response @siddontang i also encounters invalid sequence 63 != 1 while doing some select

@always-waiting
Copy link
Contributor Author

@meshenka

He means that the client of db does not support concurrency. If one client is used by many goroutines, this problem will arise

@meshenka
Copy link

meshenka commented Oct 9, 2020

@meshenka

He means that the client of db does not support concurrency. If one client is used by many goroutines, this problem will arise

ha yes ok, i. got the same issue and moved from your client to to database/sql to fix my query calls, much faster btw.

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants