add double check for stmt

This commit is contained in:
jianzhiyao 2020-06-21 17:00:45 +08:00 committed by GitHub
parent d0764bead1
commit 12b0ba7cc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,13 +14,19 @@ type PreparedStmtDB struct {
func (db *PreparedStmtDB) prepare(query string) (*sql.Stmt, error) { func (db *PreparedStmtDB) prepare(query string) (*sql.Stmt, error) {
db.mux.RLock() db.mux.RLock()
if stmt, ok := db.stmts[query]; ok { stmt, ok := db.stmts[query]
db.mux.RUnlock() db.mux.RUnlock()
if ok {
return stmt, nil return stmt, nil
} }
db.mux.RUnlock()
db.mux.Lock() db.mux.Lock()
stmt, ok = db.stmts[query]
if ok {
db.mux.Unlock()
return stmt, nil
}
stmt, err := db.ConnPool.PrepareContext(context.Background(), query) stmt, err := db.ConnPool.PrepareContext(context.Background(), query)
if err == nil { if err == nil {
db.stmts[query] = stmt db.stmts[query] = stmt