Skip to content
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

并发数超过连接池最大连接数在获取连接阻塞bug #37

Open
shenwen2012 opened this issue Dec 13, 2019 · 0 comments
Open

Comments

@shenwen2012
Copy link

shenwen2012 commented Dec 13, 2019

func query(c context.Context, tdx Tdx, queryStr string, args ...interface{}) (res *sql.Rows, err error) {
queryStr = addLimit(queryStr, 0)
queryStr, args = changeSQLIn(queryStr, args...)
start := time.Now()
if res, err = tdx.Query(queryStr, args...); err != nil {
return res, err
}
duration := time.Since(start)

var exp []*Explain
if sqlLogger.ShowExplain(duration) {
	go func() {
		exp, err := doExplain(tdx, queryStr, args...)
		if err != nil {
			return
		}
		logPrint(c, sqlLogger, exp, duration, queryStr, args...)
	}()

}
logPrint(c, sqlLogger, exp, duration, queryStr, args...)
return res, nil

}

query里执行doExplain 这个函数doExplain 应该异步执行或者去掉doExplain逻辑,否则并发query 并发数超过数据库连接池的最大连接数并sqlLogger.ShowExplain(duration) 为ture就很有可能会阻塞死。
为什么会这样呢?
tdx.Query ### 获取一个连接还没有释放,你就doExplain又去获取连接。相当于申请一个连接1,再申请连接2,释放连接2,再释放连接1.如果申请连接2的时候连接池已经满了,连接2 申请阻塞,导致连接1释放不了,连接池这种错误用法很典型。

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

1 participant