fix: double check prepare

This commit is contained in:
a631807682 2022-08-13 10:43:37 +08:00
parent 7461e04e22
commit f87bfe8c31
No known key found for this signature in database
GPG Key ID: 137D1D75522168AB

View File

@ -58,8 +58,20 @@ func (db *PreparedStmtDB) prepare(ctx context.Context, conn ConnPool, isTransact
} }
db.Mux.RUnlock() db.Mux.RUnlock()
// cache preparing stmt first
db.Mux.Lock() db.Mux.Lock()
// double check
if stmt, ok := db.Stmts[query]; ok && (!stmt.Transaction || isTransaction) {
db.Mux.Unlock()
// wait for other goroutines prepared
<-stmt.prepared
if stmt.prepareErr != nil {
return Stmt{}, stmt.prepareErr
}
return *stmt, nil
}
// cache preparing stmt first
cacheStmt := Stmt{Transaction: isTransaction, prepared: make(chan struct{})} cacheStmt := Stmt{Transaction: isTransaction, prepared: make(chan struct{})}
db.Stmts[query] = &cacheStmt db.Stmts[query] = &cacheStmt
db.Mux.Unlock() db.Mux.Unlock()