add double check for stmt
This commit is contained in:
parent
d0764bead1
commit
12b0ba7cc6
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user