From bc26d3bda6ea2bc8fd649d1843af5cd32ca8a402 Mon Sep 17 00:00:00 2001 From: xiezhaodong Date: Mon, 14 Apr 2025 20:27:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81lru=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prepare_stmt.go | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/prepare_stmt.go b/prepare_stmt.go index 624e5108..fef7e234 100644 --- a/prepare_stmt.go +++ b/prepare_stmt.go @@ -101,29 +101,33 @@ func (sdb *PreparedStmtDB) Reset() { func (db *PreparedStmtDB) prepare(ctx context.Context, conn ConnPool, isTransaction bool, query string) (Stmt, error) { db.Mux.RLock() - if stmt, ok := db.Stmts.Get(query); ok && (!stmt.Transaction || isTransaction) { - db.Mux.RUnlock() - // wait for other goroutines prepared - <-stmt.prepared - if stmt.prepareErr != nil { - return Stmt{}, stmt.prepareErr - } + if db.Stmts != nil { + if stmt, ok := db.Stmts.Get(query); ok && (!stmt.Transaction || isTransaction) { + db.Mux.RUnlock() + // wait for other goroutines prepared + <-stmt.prepared + if stmt.prepareErr != nil { + return Stmt{}, stmt.prepareErr + } - return *stmt, nil + return *stmt, nil + } } db.Mux.RUnlock() db.Mux.Lock() - // double check - if stmt, ok := db.Stmts.Get(query); ok && (!stmt.Transaction || isTransaction) { - db.Mux.Unlock() - // wait for other goroutines prepared - <-stmt.prepared - if stmt.prepareErr != nil { - return Stmt{}, stmt.prepareErr - } + if db.Stmts != nil { + // double check + if stmt, ok := db.Stmts.Get(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 + return *stmt, nil + } } // check db.Stmts first to avoid Segmentation Fault(setting value to nil map) // which cause by calling Close and executing SQL concurrently